如何在Node中的数组中访问和去重复嵌套值

时间:2018-03-22 02:17:34

标签: javascript node.js

我想通过map / reduce / lodash或其他方式以最简单的方式聚合(有效地重复数据删除)和对嵌套数据求和。可以使用ES6 / ES7没关系。最简单,最干净是首选。感谢。

我有一个数组,例如

[{
    "orderNumber": "0001",
    "itemList": [{
            "item_code": "X1000",
            "qty": 10,
            "unit_price": 20
        },
        {
            "item_code": "X1002",
            "qty": 10,
            "unit_price": 20
        }
    ]
}, {
    "orderNumber": "0002",
    "itemList": [{
            "item_code": "X1000",
            "qty": 10,
            "unit_price": 20
        },
        {
            "item_code": "X1003",
            "qty": 10,
            "unit_price": 20
        }
    ]
}]

我想最终结束;

[{
        "item_code": "X1000",
        "qty": 20,
        "unit_price": 20
    },
    {
        "item_code": "X1002",
        "qty": 10,
        "unit_price": 20
    },
    {
        "item_code": "X1003",
        "qty": 10,
        "unit_price": 20
    }
]
谢谢你的时间!

3 个答案:

答案 0 :(得分:0)

您可以使用reduce功能汇总qty

var array = [{    "orderNumber": "0001",    "itemList": [{            "item_code": "X1000",            "qty": 10,            "unit_price": 20        },        {            "item_code": "X1002",            "qty": 10,            "unit_price": 20        }    ]}, {    "orderNumber": "0002",    "itemList": [{            "item_code": "X1000",            "qty": 10,            "unit_price": 20        },        {            "item_code": "X1003",            "qty": 10,            "unit_price": 20        }    ]}];

var result = Object.values(array.reduce((a, c) => {
  c.itemList.forEach(({item_code, qty, unit_price}) => {
    (a[item_code] || (a[item_code] = {item_code, qty: 0, unit_price})).qty += qty;
  });
  
  return a;
}, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

var raw = [{
    "orderNumber": "0001",
    "itemList": [{
            "item_code": "X1000",
            "qty": 10,
            "unit_price": 20
        },
        {
            "item_code": "X1002",
            "qty": 10,
            "unit_price": 20
        }
    ]
}, {
    "orderNumber": "0002",
    "itemList": [{
            "item_code": "X1000",
            "qty": 10,
            "unit_price": 20
        },
        {
            "item_code": "X1003",
            "qty": 10,
            "unit_price": 20
        }
    ]
}];

var combined = Array.prototype.concat.apply([], raw.map(function(group){
    return group.itemList.map(function(item){
        return item;
    })
}));


var list_of_codes = [];
var noDuplicate = combined.filter(function(item){
    if (!list_of_codes.includes(item.item_code)) {
        list_of_codes.push(item.item_code);
        return true;
    }
    return false;
});

console.log(noDuplicate);

答案 2 :(得分:0)

这应该是这样的:

var data = [{
  "orderNumber": "0001",
  "itemList": [{
      "item_code": "X1000",
      "qty": 10,
      "unit_price": 20
    },
    {
      "item_code": "X1002",
      "qty": 10,
      "unit_price": 20
    }
  ]
}, {
  "orderNumber": "0002",
  "itemList": [{
      "item_code": "X1000",
      "qty": 10,
      "unit_price": 20
    },
    {
      "item_code": "X1003",
      "qty": 10,
      "unit_price": 20
    }
  ]
}]

var reduced = Object.values(data.reduce((acc, order) => {
  order.itemList.forEach((item) => {
    if (!(item.item_code in acc)) {
      acc[item.item_code] = item;
    } else {
      acc[item.item_code]['qty'] += item.qty;
    }
  });
  return acc;
}, {}));

console.log(reduced);