有一个很好的数组方法reduce()从数组中获取一个值。例子:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
在对象上实现同样效果的最佳方法是什么?我想这样做:
{
a: {value:1},
b: {value:2},
c: {value:3}
}.reduce(function(previous, current, index, array){
return previous.value + current.value;
});
但是,Object似乎没有实现任何reduce()方法。
一个对象可以通过以下方法转换为数组:object .entries(), object .keys(), object .values(),然后被还原为数组。但是,您也可以在不创建中间数组的情况下缩减对象。
我创建了一个小的帮助库odict用于处理对象。
npm install --save odict
它有一个类似于Array.prototype.reduce()的reduce函数:
export const reduce = (dict, reducer, accumulator) => {
for (const key in dict)
accumulator = reducer(accumulator, dict[key], key, dict);
return accumulator;
};
你也可以把它分配给:
Object.reduce = reduce;
因为这个方法非常有用!
所以你问题的答案是:
const result = Object.reduce(
{
a: {value:1},
b: {value:2},
c: {value:3},
},
(accumulator, current) => (accumulator.value += current.value, accumulator), // reducer function must return accumulator
{value: 0} // initial accumulator value
);
如果作为数组处理就容易多了
返回水果的总数:
let fruits = [{ name: 'banana', id: 0, quantity: 9 }, { name: 'strawberry', id: 1, quantity: 1 }, { name: 'kiwi', id: 2, quantity: 2 }, { name: 'apple', id: 3, quantity: 4 }]
let total = fruits.reduce((sum, f) => sum + f.quantity, 0);
这并不难自己实现:
function reduceObj(obj, callback, initial) {
"use strict";
var key, lastvalue, firstIteration = true;
if (typeof callback !== 'function') {
throw new TypeError(callback + 'is not a function');
}
if (arguments.length > 2) {
// initial value set
firstIteration = false;
lastvalue = initial;
}
for (key in obj) {
if (!obj.hasOwnProperty(key)) continue;
if (firstIteration)
firstIteration = false;
lastvalue = obj[key];
continue;
}
lastvalue = callback(lastvalue, obj[key], key, obj);
}
if (firstIteration) {
throw new TypeError('Reduce of empty object with no initial value');
}
return lastvalue;
}
在行动:
var o = {a: {value:1}, b: {value:2}, c: {value:3}};
reduceObj(o, function(prev, curr) { prev.value += cur.value; return prev;}, {value:0});
reduceObj(o, function(prev, curr) { return {value: prev.value + curr.value};});
// both == { value: 6 };
reduceObj(o, function(prev, curr) { return prev + curr.value; }, 0);
// == 6
你也可以把它添加到Object原型中:
if (typeof Object.prototype.reduce !== 'function') {
Object.prototype.reduce = function(callback, initial) {
"use strict";
var args = Array.prototype.slice(arguments);
args.unshift(this);
return reduceObj.apply(null, args);
}
}
由于它还没有在答案中得到证实,因此下划线的减少也适用于此。
_.reduce({
a: {value:1},
b: {value:2},
c: {value:3}
}, function(prev, current){
//prev is either first object or total value
var total = prev.value || prev
return total + current.value
})
注意,_。如果列表对象只有一项,Reduce将返回唯一的值(对象或其他),而不调用迭代器函数。
_.reduce({
a: {value:1}
}, function(prev, current){
//not called
})
//returns {value: 1} instead of 1