对象上的 Javascript reduce()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15748656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript reduce() on Object
提问by Pavel S.
There is nice Array method reduce()
to get one value from the Array. Example:
有一个很好的 Array 方法reduce()
可以从 Array 中获取一个值。例子:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
What is the best way to achieve the same with objects? I'd like to do this:
用对象实现相同目标的最佳方法是什么?我想这样做:
{
a: {value:1},
b: {value:2},
c: {value:3}
}.reduce(function(previous, current, index, array){
return previous.value + current.value;
});
However, Object does not seem to have any reduce()
method implemented.
但是, Object 似乎没有reduce()
实现任何方法。
采纳答案by Daniel Bayley
What you actually want in this case are the Object.values
. Here is a concise ES6implementation with that in mind:
在这种情况下,您真正想要的是Object.values
. 考虑到这一点,这是一个简洁的ES6实现:
const add = {
a: {value:1},
b: {value:2},
c: {value:3}
}
const total = Object.values(add).reduce((t, {value}) => t + value, 0)
console.log(total) // 6
or simply:
或者干脆:
const add = {
a: 1,
b: 2,
c: 3
}
const total = Object.values(add).reduce((t, n) => t + n)
console.log(total) // 6
回答by Jonathan Lonowski
One option would be to reduce
the keys()
:
一个办法是到reduce
了keys()
:
var o = {
a: {value:1},
b: {value:2},
c: {value:3}
};
Object.keys(o).reduce(function (previous, key) {
return previous + o[key].value;
}, 0);
With this, you'll want to specify an initial value or the 1st round will be 'a' + 2
.
有了这个,您需要指定一个初始值,否则第一轮将是'a' + 2
.
If you want the result as an Object ({ value: ... }
), you'll have to initialize and return the object each time:
如果您希望结果为 Object ( { value: ... }
),则每次都必须初始化并返回该对象:
Object.keys(o).reduce(function (previous, key) {
previous.value += o[key].value;
return previous;
}, { value: 0 });
回答by faboulaws
ES6 implementation:Object.entries()
ES6 实现:Object.entries()
const o = {
a: {value: 1},
b: {value: 2},
c: {value: 3}
};
const total = Object.entries(o).reduce(function (total, pair) {
const [key, value] = pair;
return total + value;
}, 0);
回答by ZER0
First of all, you don't quite get what's reduce's previous value is.
首先,您不太明白reduce的先前值是什么。
In you pseudo code you have return previous.value + current.value
, therefore the previous
value will be a number on the next call, not an object.
在您的伪代码中return previous.value + current.value
,因此该previous
值将是下一次调用时的数字,而不是对象。
Second, reduce
is an Array method, not an Object's one, and you can't rely on the order when you're iterating the properties of an object (see: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in, this is applied to Object.keystoo); so I'm not sure if applying reduce
over an object makes sense.
其次,reduce
是 Array 方法,而不是 Object 的方法,并且在迭代对象的属性时不能依赖顺序(请参阅:https: //developer.mozilla.org/en-US/docs/ JavaScript/Reference/Statements/for...in,这也适用于Object.keys);所以我不确定reduce
在对象上应用是否有意义。
However, if the order is not important, you can have:
但是,如果顺序不重要,您可以:
Object.keys(obj).reduce(function(sum, key) {
return sum + obj[key].value;
}, 0);
Or you can just mapthe object's value:
或者你可以只映射对象的值:
Object.keys(obj).map(function(key) { return this[key].value }, obj).reduce(function (previous, current) {
return previous + current;
});
P.S. in ES6 with the fat arrow function's syntax (already in Firefox Nightly), you could shrink a bit:
PS 在 ES6 中使用粗箭头函数的语法(已经在 Firefox Nightly 中),你可以缩小一点:
Object.keys(obj).map(key => obj[key].value).reduce((previous, current) => previous + current);
回答by user1247458
Extend Object.prototype.
扩展 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 );
};
Sample of using.
使用示例。
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; '.
n'Joy it, guys!! ;-)
不高兴,伙计们!!;-)
回答by Alair Tavares Jr
1:
1:
[{value:5}, {value:10}].reduce((previousValue, currentValue) => { return {value: previousValue.value + currentValue.value}})
>> Object {value: 15}
2:
2:
[{value:5}, {value:10}].map(item => item.value).reduce((previousValue, currentValue) => {return previousValue + currentValue })
>> 15
3:
3:
[{value:5}, {value:10}].reduce(function (previousValue, currentValue) {
return {value: previousValue.value + currentValue.value};
})
>> Object {value: 15}
回答by Janus Troelsen
You can use a generator expression (supported in all browsers for years now, and in Node) to get the key-value pairs in a list you can reduce on:
您可以使用生成器表达式(多年来在所有浏览器中以及在 Node 中都支持)来获取可以减少的列表中的键值对:
>>> a = {"b": 3}
Object { b=3}
>>> [[i, a[i]] for (i in a) if (a.hasOwnProperty(i))]
[["b", 3]]
回答by Igor Sukharev
An object can be turned into an array with: Object.entries(), Object.keys(), Object.values(), and then be reduced as array. But you can also reduce an object without creating the intermediate array.
一个对象可以变成一个数组:Object.entries(),Object.keys(),Object.values(),然后减少为数组。但是您也可以在不创建中间数组的情况下减少对象。
I've created a little helper library odictfor working with objects.
我创建了一个小助手库odict来处理对象。
npm install --save odict
It has reduce
function that works very much like Array.prototype.reduce():
它的reduce
功能与Array.prototype.reduce()非常相似:
export const reduce = (dict, reducer, accumulator) => {
for (const key in dict)
accumulator = reducer(accumulator, dict[key], key, dict);
return accumulator;
};
You could also assign it to:
您还可以将其分配给:
Object.reduce = reduce;
as this method is very useful!
因为这个方法非常有用!
So the answer to your question would be:
所以你的问题的答案是:
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
);
回答by kennebec
If you can use an array, do use an array, the length and order of an array are half its worth.
如果可以使用数组,请使用数组,数组的长度和顺序是其价值的一半。
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);
/* returned value: (Number) 6 */
/* 返回值:(数字)6 */
回答by Francis Avila
This is not very difficult to implement yourself:
这不是很难自己实现:
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;
}
In action:
在行动:
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
You can also add it to the Object prototype:
您还可以将其添加到 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);
}
}