Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript减少数组中对象的多个键_Javascript_Arrays_Underscore.js_Lodash_Reduce - Fatal编程技术网

javascript减少数组中对象的多个键

javascript减少数组中对象的多个键,javascript,arrays,underscore.js,lodash,reduce,Javascript,Arrays,Underscore.js,Lodash,Reduce,我有以下类型的对象映射(下面的示例数据) 解决了 var x = _.reduce( list, (acc, item) => { for (const key in item) { if (!acc[key]) { acc[key] = { price: 0, amount: 0 }; } if (item[key] && item[k

我有以下类型的对象映射(下面的示例数据)

解决了

var x = _.reduce(
    list,
    (acc, item) => {
        for (const key in item) {
            if (!acc[key]) {
                acc[key] = { price: 0, amount: 0 };
            }

            if (item[key] && item[key].price) {
                acc[key].price += item[key].price;
            }

            if (item[key] && item[key].amount) {
                acc[key].amount += item[key].amount;
            }
        }
        return acc;
    },
    {}
);
您可以使用转换对象
数据
对象的每个值。要组合具有相同键值的所有对象,如果要计算要转换的键,则可以使用回调测试

const result = _.mapValues(data, collection => 
  _.mergeWith({}, ...collection, (a = 0, b = 0, key) =>
    ['amount', 'price'].includes(key)
      ? a + b
      : void 0 // is equivalent to undefined to retain current value
  ));
const数据={
“2018-08-01T11:00:00+02:00”:[
{
“BHKW”:{
“金额”:131,
“价格”:85
},
“撤退”:{
“金额”:84,
“价格”:1
}
},
{
“BHKW”:{
“金额”:307,
“价格”:58
},
“PV”:{
“金额”:4,
“价格”:60
}
}
],
“2018-08-01T12:00:00+02:00”:[
{
“BHKW”:{
“金额”:288,
“价格”:59
},
“PV”:{
“金额”:742,
“价格”:73
}
},
{
“BHKW”:{
“金额”:250,
“价格”:49
},
“PV”:{
“金额”:507,
“价格”:98
}
},
{
“PV”:{
“金额”:368,
“价格”:22
},
“BHKW”:{
“金额”:357,
“价格”:73
}
},
{
“撤退”:{
“金额”:135,
“价格”:62
},
“BHKW”:{
“金额”:129,
“价格”:93
}
}
]
};
const result=\映射值(数据,集合=>
_.mergeWith({},…集合,(a=0,b=0,key)=>
[‘金额’、‘价格’]。包括(键)
?a+b
:void 0//相当于未定义以保留当前值
));
控制台日志(结果)
。作为控制台包装{最小高度:100%;顶部:0}
另一种方法(尽管不像已经发布的
.merge
方法那样优雅)是使用
.mapValues/\u0.reduce/\u0.assignWith或0.extendWith

var数据={“2018-08-01T11:00:00+02:00”:[{“BHKW”:{“金额”:131,“价格”:85},“回退”:{“金额”:84,“价格”:1},{“BHKW”:{“金额”:307,“价格”:58},“PV”:{“金额”:4,“价格”:60},““2018-08-01T12:00:00+02:00”:[{“BHKW”:{“金额”:288,“价格”:59},{“金额”:742,“价格”:73},{“金额”::250,“价格”:49},“PV”:{“金额”:507,“价格”:98},{“PV”:{“金额”:368,“价格”:22},“BHKW”:{“金额”:357,“价格”:73},{“回退”:{“金额”:135,“价格”:62},“BHKW”:{“金额”:129,“价格”:93}}
var结果=
_.mapValues(数据,(x)=>
_.减少(x,(r,c)=>
_.与(r,c,(o,s)=>
o&s?({金额:o.amount+s.amount,价格:o.price+s.price}):{…o,…s})
)
)
console.log(结果)
var x = _.reduce(
    list,
    (acc, item) => {
        for (const key in item) {
            if (!acc[key]) {
                acc[key] = { price: 0, amount: 0 };
            }

            if (item[key] && item[key].price) {
                acc[key].price += item[key].price;
            }

            if (item[key] && item[key].amount) {
                acc[key].amount += item[key].amount;
            }
        }
        return acc;
    },
    {}
);
const result = _.mapValues(data, collection => 
  _.mergeWith({}, ...collection, (a = 0, b = 0, key) =>
    ['amount', 'price'].includes(key)
      ? a + b
      : void 0 // is equivalent to undefined to retain current value
  ));