使用对象扩展运算符添加多个对象(ES6,JavaScript)

时间:2017-11-29 10:28:08

标签: javascript object ecmascript-6 spread-syntax

我有3个具有相同数据的对象,但内部数组具有单独的服务和商品ID,所以我试图得到如下所述的预期结果please check my try here。提前致谢

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

对象3:

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

最终结果 - 每个对象都有单独的服务,然后提供相同的offerid& serviceid需要添加count + 1否则返回数据

  const result = {

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

              }

2 个答案:

答案 0 :(得分:2)

您可以使用array#reduce将所有对象合并为单个对象,并array#concat services值。然后使用array#reduce根据对象中的service_id合并所有服务对象,并将此对象的值重新分配给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)

答案 1 :(得分: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));