有一个很好的数组方法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.prototype。

Object.prototype.reduce = function( reduceCallback, initialValue ) {
    var obj = this, keys = Object.keys( obj );

    return keys.reduce( function( prevVal, item, idx, arr ) {
        return reduceCallback( prevVal, item, obj[item], obj );
    }, initialValue );
};

使用示例。

var dataset = {
    key1 : 'value1',
    key2 : 'value2',
    key3 : 'value3'
};

function reduceFn( prevVal, key, val, obj ) {
    return prevVal + key + ' : ' + val + '; ';
}

console.log( dataset.reduce( reduceFn, 'initialValue' ) );
'Output' == 'initialValue; key1 : value1; key2 : value2; key3 : value3; '.

高兴吧,伙计们!!: -)

其他回答

如果你可以使用数组,那就使用数组,数组的长度和顺序是它价值的一半。

function reducer(obj, fun, temp){
    if(typeof fun=== 'function'){
        if(temp== undefined) temp= '';
        for(var p in obj){
            if(obj.hasOwnProperty(p)){
                temp= fun(obj[p], temp, p, obj);
            }
        }
    }
    return temp;
}
var O={a:{value:1},b:{value:2},c:{value:3}}

reducer(O, function(a, b){return a.value+b;},0);

/*返回值:(Number) 6 * /

首先,你不太清楚reduce之前的值是什么。

在你的伪代码中,你已经返回先前的。值+电流。值,因此前一个值在下一次调用时将是一个数字,而不是一个对象。

其次,reduce是一个Array方法,而不是Object的方法,当你迭代一个对象的属性时,你不能依赖顺序(参见:https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in,这应用于Object。键);所以我不确定在对象上应用reduce是否有意义。

然而,如果顺序不重要,你可以有:

Object.keys(obj).reduce(function(sum, key) {
    return sum + obj[key].value;
}, 0);

或者你可以映射对象的值:

Object.keys(obj).map(function(key) { return this[key].value }, obj).reduce(function (previous, current) {
    return previous + current;
});

附注:在ES6中使用胖箭头函数的语法(已经在Firefox Nightly中),你可以缩小一点:

Object.keys(obj).map(key => obj[key].value).reduce((previous, current) => previous + current);

试试这个线性箭头函数

Object.values(o).map(a => a.value, o).reduce((ac, key, index, arr) => ac+=key)

一个对象可以通过以下方法转换为数组: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
);

由于它还没有在答案中得到证实,因此下划线的减少也适用于此。

_.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