最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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

              }

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

              }
Share Improve this question asked Nov 29, 2017 at 10:28 Narayanan SNarayanan S 1271 gold badge5 silver badges14 bronze badges 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. – apokryfos 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 – Narayanan S Commented Nov 29, 2017 at 10:55
  • Check my answer. It's a solution, but not sure if it qualifies as a "good solution" – apokryfos Commented Nov 29, 2017 at 11:07
Add a ment  | 

3 Answers 3

Reset to default 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 bined = [obj1, obj2, obj3].reduce((r,o) => Object.assign({}, o, {services : r.services.concat(o.services)}));

bined.services = Object.values(bined.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(bined)

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/rkdejpab/15/

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));
Post a comment

comment list (0)

  1. No comments so far