我有这样的数据:
var data = [
{2: 1, 6: 1},
{2: 2},
{1: 3, 6: 2},];
( "2“表示key,"1”表示"count")
我想像这样输出:
output = [
{2: 3, 6: 3, 1: 3}, ];
有没有办法通过使用lodash对其进行归档?
发布于 2018-03-05 17:01:55
使用带有扩展的_.mergeWith()合并所有键并对它们的值求和:
const data = [{2: 1, 6: 1}, {2: 2}, {1: 3, 6: 2}];
const result = _.mergeWith({}, ...data, (objValue, srcValue) =>
_.isNumber(objValue) ? objValue + srcValue : srcValue);
console.log(result);<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
https://stackoverflow.com/questions/49106572
复制相似问题