JavaScript reduce() na obiekcie
Istnieje metoda nice Array reduce()
aby uzyskać jedną wartość z tablicy. Przykład:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
Jaki jest najlepszy sposób na osiągnięcie tego samego z przedmiotami? Chciałbym to zrobić:
{
a: {value:1},
b: {value:2},
c: {value:3}
}.reduce(function(previous, current, index, array){
return previous.value + current.value;
});
Wydaje się jednak, że obiekt nie posiada zaimplementowanej metody reduce()
.
10 answers
Jedną z opcji byłoby 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);
Z tym, będziesz chciał podać wartość początkową lub pierwsza runda będzie 'a' + 2
.
Jeśli chcesz, aby wynik był obiektem ({ value: ... }
), musisz za każdym razem inicjalizować i zwracać obiekt:
Object.keys(o).reduce(function (previous, key) {
previous.value += o[key].value;
return previous;
}, { value: 0 });
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 18:08:57
Wdrożenie ES6: obiekt.wpisy()
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-31 14:40:47
Po pierwsze, nie do końca rozumiesz co to jest reduce's poprzednia wartość jest.
W Twoim pseudo kodzie masz return previous.value + current.value
, dlatego wartość previous
będzie liczbą przy następnym wywołaniu, a nie obiektem.
Po Drugie, reduce
jest metodą tablicową, a nie obiektową i nie można polegać na kolejności, gdy iterujesz właściwości obiektu (zobacz: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Statements/for...in , stosuje się to do obiekt.keys too); więc nie jestem pewien, czy stosowanie reduce
nad obiektem ma sens.
Jeśli jednak zamówienie nie jest ważne, możesz mieć:
Object.keys(obj).reduce(function(sum, key) {
return sum + obj[key].value;
}, 0);
Lub możesz po prostu odwzorować wartość obiektu:
Object.keys(obj).map(function(key) { return this[key].value }, obj).reduce(function (previous, current) {
return previous + current;
});
P. S. w ES6 ze składnią funkcji fat arrow (już w Firefoxie Nightly) można by trochę się skurczyć:
Object.keys(obj).map(key => obj[key].value).reduce((previous, current) => previous + current);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 18:16:07
To, czego naprawdę chcesz w tym przypadku, to Object.values
. Oto zwięzła implementacja ES6 z myślą o tym:
add = {
a: {value:1},
b: {value:2},
c: {value:3}
}
total = Object.values(add).reduce((t, n) => t + n.value, 0)
console.log(total) // 6
Lub po prostu:
add = {
a: 1,
b: 2,
c: 3
}
total = Object.values(add).reduce((t, n) => t + n)
console.log(total) // 6
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-19 14:54:54
Extend Object.prototyp.
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 );
};
Próbka użycia.
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; '.
Nie Bawcie się, chłopaki!! ;-)Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-05 13:41:48
To nie jest bardzo trudne do realizacji siebie:
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;
}
W akcji:
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
Można również dodać go do prototypu obiektu:
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);
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 19:01:11
1:
[{value:5}, {value:10}].reduce((previousValue, currentValue) => { return {value: previousValue.value + currentValue.value}})
>> Object {value: 15}
2:
[{value:5}, {value:10}].map(item => item.value).reduce((previousValue, currentValue) => {return previousValue + currentValue })
>> 15
3:
[{value:5}, {value:10}].reduce(function (previousValue, currentValue) {
return {value: previousValue.value + currentValue.value};
})
>> Object {value: 15}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-09 17:34:33
Jeśli możesz użyć tablicy, użyj tablicy, długość i kolejność tablicy są o połowę jej wartości.
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);
/ * zwracana wartość: (Liczba) 6 */
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-01 18:22:50
Możesz użyć wyrażenia generatora (obsługiwanego od lat we wszystkich przeglądarkach i w Node), aby uzyskać pary klucz-wartość na liście, którą możesz zmniejszyć:
>>> a = {"b": 3}
Object { b=3}
>>> [[i, a[i]] for (i in a) if (a.hasOwnProperty(i))]
[["b", 3]]
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-09 17:02:38
Ponieważ nie zostało to jeszcze potwierdzone w odpowiedzi, reduce
również działa na to.
_.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
})
Uwaga, _.reduce
zwróci jedyną wartość (obiekt lub w inny sposób), jeśli obiekt list ma tylko jedną pozycję, bez wywoływania funkcji iteratora.
_.reduce({
a: {value:1}
}, function(prev, current){
//not called
})
//returns {value: 1} instead of 1
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-01 01:25:01