Home > Mobile >  How to place an object as an item of a Javascript array?
How to place an object as an item of a Javascript array?

Time:02-26

I have a code that returns the following list from an API:

0: {idProcess: 491, serviceName: 'Testing', UserName: 'Mark', status: 'Delayed', idClient: 1, …} 
1: {idProcess: 517, serviceName: 'FGTS Total', UserName: 'Joseph', status: 'Ok', idClient: 1, …}
2: {idProcess: 493, serviceName: 'Testing', UserName: 'Mark', status: 'Delayed', idClient: 1, …}
3: {idProcess: 519, serviceName: 'Portability', UserName: 'Joseph', status: 'Ok', idClient: 1, …}
4: {idProcess: 490, serviceName: 'Deed', UserName: 'Philip', status: 'Delayed', idClient: 1, …}
5: {idProcess: 492, serviceName: 'Approval', UserName: 'Philip', status: 'Delayed', idClient: 1, …}
6: {idProcess: 510, serviceName: 'Financing', UserName: 'John', status: 'Delayed', idClient: 1, …}
7: {idProcess: 515, serviceName: 'Aprovacao', UserName: 'John', status: 'Delayed', idClient: 1, …}
8: {idProcess: 518, serviceName: 'Zzz', UserName: 'Marcio ', status: 'Ok', idClient: 1, …}
9: {idProcess: 19, serviceName: 'Deed', UserName: 'Marcio ', status: 'Delayed', idClient: 1, …}
10: {idProcess: 13, serviceName: 'Deed', UserName: 'Mark', status: 'Ok', idClient: 1, …}
11: {idProcess: 12, serviceName: 'Financing', UserName: 'Mark', status: 'Delayed', idClient: 1, …}
12: {idProcess: 514, serviceName: 'Approval', UserName: 'Joseph', status: 'Delayed', idClient: 1, …}
13: {idProcess: 489, serviceName: 'Consortium', UserName: 'Mark', status: 'Ok', idClient: 1, …}
length: 14

Each object within this array I will call PROCESS, I needed to get each of these service names and store it in another list, as well as the amount of each serviceName that appears in the list. I managed to do this using the code below:

  let servicesList= [];
  resp.forEach((item) => {
    const service = servicesList.find(
      (serv) => serv.serviceName === item.serviceName
    );
    if (service) {
      service.quantity  ;
    } else {
      servicesList.push({
        serviceName: item.serviceName,
        quantity: 1,
      });
    }
  });

This code snippet returned me another list like this:

0: {naneService: 'Testing', quantity: 2}
1: {nameService: 'FGTS Total', quantity: 1}
etc...

I now need to insert each PROCESS of this list corresponding to nameService as one more item in the second array created and I don't know how to do that, below is a model of how I would like the new list to be:

[
{nameService: "Testing", quantity: 2, process: [{idProcess: 491, nameService:
'Testing', UserName: 'Mark', status: 'Delayded', idClient: 1, …}, {idProcess: 493,
ServiceName: 'Testing', Username: 'Mark', status: 'Delayed', idClient: 1, …}]
nameService: "FGTS Total", quantity: 1, process: [{idProcess: 517, serviceName: 'FGTS Total',
UserName: 'Joseph', status: 'Ok', idClient: 1, …}]}
  ]

CodePudding user response:

I think you can utilize your existing loop for this.

      let servicesList= [];
      resp.forEach((item) => {
        const serviceIndex = servicesList.findIndex(
          (serv) => serv.serviceName === item.serviceName
        );
        if (serviceIndex>=0) {
          serviceList[serviceIndex].quantity  ;
serviceList[serviceIndex].process.push(item)
        } else {
          servicesList.push({
            serviceName: item.serviceName,
            quantity: 1,
          });
        }
      });

CodePudding user response:

Please try to return the array inside the function, read the documentation about the let and const keywork.

NameArray.map((el,i) => {
let new_array = [];
return new_array.push = { 
key:idProcess, 
payload : el.serviceName 
}})

CodePudding user response:

// simplified response example
const resp = [{id: 1, nameService: 'alpha'}, {id: 2, nameService: 'beta'}, {id: 3, nameService: 'alpha'}]

// grouping processes by their nameService
const groups = {}
resp.forEach(process => {
  const group = groups[process.nameService]
  if (group) {
    group.quantity  = 1;
    group.processes.push(process)
  } else {
    groups[process.nameService] = {
      quantity: 1,
      nameService: process.nameService,
      processes: [process]
    }
  }
})


const result = Object.values(groups)
console.log(result)

CodePudding user response:

array.reduce() is what you are looking for:

const processes = [...yourArray];

const newProcesses = processes.reduce((acc, process) => {
  if (acc.some((item) => item.serviceName === process.serviceName)) {
    return acc;
  }

  const matchingProcesses = processes.filter((item) => item.serviceName === process.serviceName);

  return [
    ...acc,
    {
      serviceName: process.serviceName,
      quantity: matchingProcesses.length,
      process: matchingProcesses
    }
  ]
}, []);
  • Related