Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding multiple objects using object spread operator (ES6, JavaScript)

i have 3 objects having same data but inside array having separate service and offer id so i tried to get expected result as below mentioned and please check my try here . Thanks in advance

Object 1:

const obj1 = {
              bid              : 1,
              mobile           : 9533703390,
              services : [
                  {
                   service_id  : 5,
                   offer_id    : 10,
                   count       : 1
                  }
              ]
        }

object2 :

const obj2 = {
              bid              : 1,
              mobile           : 9524703390,
              services : [
                  {
                   service_id  : 8,
                   offer_id    : 12,
                   count       : 1
                  }
              ]
        }

object 3:

const obj3 = {
              bid              : 1,
              mobile           : 9524703390,
              services : [
                  {
                   service_id  : 5,
                   offer_id    : 10,
                   count       : 1
                  }
              ]
        }

Final Result - each object having separate services and offer then if same offerid & serviceid came need to add count + 1 otherwise return data

  const result = {

                 bid              : 1,
                 mobile           : 9524703390,
                 services : [
                    {
                      service_id  : 5,
                      offer_id    : 10,
                      count       : 2
                    },
                    {
                      service_id  : 8,
                      offer_id    : 12,
                      count       : 1
                    }
                 ]

              }
like image 957
Narayanan S Avatar asked Nov 29 '17 10:11

Narayanan S


3 Answers

You can use array#reduce to merge all objects into single object and array#concat the services values. Then use array#reduce to merge all service object based on service_id in an object and reassign the values of this object to services.

const obj1 = { bid : 1, mobile : 9533703390, services : [ { service_id : 5, offer_id : 10, count : 1 } ] },
      obj2 = { bid : 1, mobile : 9524703390, services : [ { service_id : 8, offer_id : 12, count : 1 } ] },
      obj3 = { bid : 1, mobile : 9524703390, services : [ { service_id : 5, offer_id : 12, count : 1 } ] };

var combined = [obj1, obj2, obj3].reduce((r,o) => Object.assign({}, o, {services : r.services.concat(o.services)}));

combined.services = Object.values(combined.services.reduce((res, services) => {
  if(res[services.service_id])
    res[services.service_id].count += services.count;
  else
    res[services.service_id] = Object.assign({}, services);
    return res;
},{}));

console.log(combined)
like image 50
Hassan Imam Avatar answered Nov 05 '22 00:11

Hassan Imam


The spread operator doesn't do recursive appending or anything like that. You can however use it in conjunction with Object.assign like so:

const result = Object.assign(
 obj1, 
 obj2, 
 obj3, { 
  services: [ ...obj1.services, ...obj2.services, ...obj3.services,  ]
}); //Consolidate everything

Then you can consolidate the services:

const servicesObject = {};
result.services.forEach((service) => {
        if (servicesObject[service.service_id] !== undefined) {
            servicesObject[service.service_id].count += 1;
    } else {
          servicesObject[service.service_id] = service;
    }
}); // Merge services
result.services = servicesObject;

If you still want the services to be an array then you can do

result.services = Object.entries(servicesObject)
                      .map(([key,value]) => value);

Check the updated fiddle http://jsfiddle.net/rkdejpab/15/

like image 43
apokryfos Avatar answered Nov 05 '22 00:11

apokryfos


function merge(...objs) {

    let result = objs.reduce((acc, item) => {
        acc.bid = item.bid;
        acc.mobile = item.mobile;
        if (!acc.services) {
            acc.services = []
            acc.services.push(item.services[0]);
        }
        else {
            let index = acc.services.findIndex(function (elem) {
                return elem.service_id === item.services[0].service_id && elem.offer_id == item.services[0].offer_id;
            });
            if (!(index === -1)) {
                acc.services[index].count += 1;
            }
            else {
                acc.services.push(item.services[0]);
            }
        }
            return acc
    }, {});
    return result;
}

console.log(merge(obj1, obj2, obj3));
like image 1
zabusa Avatar answered Nov 05 '22 02:11

zabusa