Mapreduce 如何使用reduce(下划线?)对对象数组中的某些值求和
我有以下JSON结构Mapreduce 如何使用reduce(下划线?)对对象数组中的某些值求和,mapreduce,underscore.js,Mapreduce,Underscore.js,我有以下JSON结构 var fooArray = [{ name: 'firstValue', price1: 20, price2: 11 }, { name: 'secondValue', price1: 54, price2: 13 }, { name: 'thirdValue', price1: 3, price2: 6 }] 如何对对象数组中的值求和?(使用进行循环除外)
var fooArray =
[{ name: 'firstValue',
price1: 20,
price2: 11
},
{ name: 'secondValue',
price1: 54,
price2: 13
},
{ name: 'thirdValue',
price1: 3,
price2: 6
}]
如何对对象数组中的值求和?(使用<代码>进行<代码>循环除外)
您可以使用一个对属性求和的函数对数组进行缩减:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
});
您可以使用一个对属性求和的函数对数组进行缩减:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
});
您可以使用一个对属性求和的函数对数组进行缩减:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
});
您可以使用一个对属性求和的函数对数组进行缩减:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
});
这与@matthewmcviegh的答案类似,但生成的对象没有虚假的
名称
属性:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
}, {price1: 0, price2: 0});
这与@matthewmcviegh的答案类似,但生成的对象没有虚假的
名称
属性:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
}, {price1: 0, price2: 0});
这与@matthewmcviegh的答案类似,但生成的对象没有虚假的
名称
属性:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
}, {price1: 0, price2: 0});
这与@matthewmcviegh的答案类似,但生成的对象没有虚假的
名称
属性:
_.reduce(fooArray, function (accum, x) {
return {
price1: x.price1 + accum.price1,
price2: x.price2 + accum.price2
};
}, {price1: 0, price2: 0});