5

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
                    }
                 ]

              }
3
  • 1
    The spread operator is not magic. It can't decide how to merge or when recursion is needed. You need to do a manual iteration of services to merge them correctly. Commented Nov 29, 2017 at 10:49
  • can you please provide some example . i'm new to java script since i'm trying for good solution Commented Nov 29, 2017 at 10:55
  • Check my answer. It's a solution, but not sure if it qualifies as a "good solution" Commented Nov 29, 2017 at 11:07

3 Answers 3

4

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Hi how to merge all service object based on service_id && offer_id ?
You can use a combined key inside the combined object. One possible key could be services.service_id + '|' + services.offer_id.
1

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/

Comments

1
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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.