简体   繁体   中英

Combine arrays to one array

lets say i have the following array, how can i achieve the last given example in my this question by modifing the given reduce function?

i have tried to assign [cur] to a plans key but it only puts the first element out.

i have the feeling it have something to do with the way i concat the objects but i cant figure it out on my own.

{
   employee: 'employee_1',
   customer: {
     name: 'customer_1',
   },
   startdate: '2020-03-01T23:00:00.000Z'
}, {
   employee: 'employee_2',
   customer: {
     name: 'customer_1',
   },
   startdate: '2020-03-01T23:00:00.000Z'
}, {
   employee: 'employee_3',
   customer: {
     name: 'customer_1',
   },
   startdate: '2020-03-01T23:00:00.000Z'
}
plans.reduce(function(o, cur) {

   // Get the index of the key-value pair.
   var occurs = o.reduce(function(n, item, i) {
      return (item.customer.toString() === cur.customer.toString()) ? i : n;
   }, -1);

   // If the name is found,
   if (occurs >= 0) {
      // append the current value to its list of values.
      o[occurs].employee = o[occurs].employee.concat(cur.employee)
      o[occurs].startdate = o[occurs].startdate.concat(cur.startdate)

   // Otherwise
   } else {

      // add the current item to o (but make sure the value is an array).
      var obj = {
         customer: cur.customer,
         employee: [cur.employee],
         startdate: [cur.startdate]
      };
      o = o.concat([obj]);
   }
   return o;
}, [])

this reduces the given array to something like this:

{
   customer: {
     name: 'customer_1'
   },
   employee: [{
     employee: 'employee_1'
   }, {
     employee: 'employee_2'
   }, {
     employee: 'employee_3'
   }],
   startdate: [{
     startdate: '2020-03-01T23:00:00.000Z'
   }, {
     startdate: '2020-03-01T23:00:00.000Z'
   }, {
     startdate: '2020-03-01T23:00:00.000Z'
   }]  
}

But what i need is something like this:

{
   customer: {customer_data},
   plans: [{
     employee: 'employee_1',
     startdate: '2020-03-01T23:00:00.000Z' 
   }, {
     employee: 'employee_2',
     startdate: '2020-03-01T23:00:00.000Z' 
   }, {
     employee: 'employee_3',
     startdate: '2020-03-01T23:00:00.000Z' 
   }]
}

I modified your solution so as to get the desired output .

Instead of using reduce , you should use find method to check if customer already exists in the array . Also you were not taking into consideration the plans array , I have added that.

 var plans = [ { employee: 'employee_1', customer: { name: 'customer_1', }, startdate: '2020-03-01T23:00:00.000Z' }, { employee: 'employee_2', customer: { name: 'customer_1', }, startdate: '2020-03-01T23:00:00.000Z' }, { employee: 'employee_3', customer: { name: 'customer_1', }, startdate: '2020-03-01T23:00:00.000Z' } ]; var result = plans.reduce(function(o, cur) { // Get the index of the key-value pair. var occurs = o.find(ob=> ob.customer.name === cur.customer.name); // If the name is found, if (occurs) { // append the current value to its list of values. occurs.plans.push( {employee: cur.employee, startdate: cur.startdate} ) // Otherwise } else { // add the current item to o (but make sure the value is an array). var obj = { customer: cur.customer, plans:[ {employee: cur.employee, startdate: cur.startdate} ] }; o = o.concat(obj); } return o; }, []) console.log(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM