Stack Overflow Asked by AskMen on November 20, 2021
I want to get the sum of all array items.
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }];
const app = (arr) => {
const r = arr.reduce((acc, nextValue) => {
return acc[Object.keys(acc)] + nextValue[Object.keys(nextValue)]
})
return r
}
console.log(app(arr))
So, at the end i want to get: 8
= sum of: { a: 1 }, { b: 5 }, { c: 2 }
.
Question: Why now i get NaN
as a result?
Let's first understand what reduce
is?
Consider this following custom implementation:
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }];
const reduced = (array, instructionToCombine, buildingUpOn) => {
for (let i = 0; i < array.length; i++) {
buildingUpOn = instructionToCombine(buildingUpOn, array[i][Object.keys(array[i])]);
}
return buildingUpOn;
}
const instructionToCombine = (a, b) => a + b;
const ans = reduced(arr, instructionToCombine, 0);
console.log(ans)
Now, you got how JavaScript default reduce
method works.
You're missing the initialValue
(buildingUpOn
in my implementation). Know that, initialValue
is not required parameter, rather it's optional. Yet,
If no initialValue is supplied, the first element in the array will be used as the initial accumulator value and skipped as currentValue - MDN
But you treated otherwise that's why didn't get the expected answer. acc
is a object for the first iteration, but rest of the iteration it's a number but you treated like a object which returned undefined and, sum of a Number
with undefined
returns NaN
.
If you still need to achieve this without passing the initialValue
, it's very possible by keeping acc
an object this way:
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }];
const app = (arr) => {
const r = arr.reduce((acc, nextValue) => {
const newValue = acc[Object.keys(acc)] + nextValue[Object.keys(nextValue)];
return {x: newValue};
})
return r[Object.keys(r)];
}
console.log(app(arr))
Answered by Mateen on November 20, 2021
arr.map(obj => Object.values(obj)).flat().reduce((acc, next) => acc += next)
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }]
const arrayMap = arr.map(obj => Object.values(obj)).flat().reduce((acc, next) => acc += next)
console.log(arrayMap)
Answered by tnanhpt on November 20, 2021
This is so simple, you making it difficult. You need values not keys and to make sure use Number()
function
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }];
const app = (arr) => {
const r = arr.reduce((acc, nextValue) => {
return acc += Number(Object.values(nextValue))
},0)
return r
}
console.log(app(arr))
Answered by DEEPAK on November 20, 2021
It is good to point that you want to return a number from reducer, and out initial value type number as a second parameter in your function
const app = (arr) => {
const r = arr.reduce((acc, nextValue) => {
console.log(nextValue[Object.keys(nextValue)]);
return acc + nextValue[Object.keys(nextValue)];
}, 0);
return r;
};
console.log(app(arr));
Answered by Anna Vlasenko on November 20, 2021
you need to add initialValue 0 to reduce
const app = (arr) => arr.reduce((acc, nextValue) => {
return acc + Object.values(nextValue)[0]
} ,0)
console.log(app(arr))
Answered by Moufeed Juboqji on November 20, 2021
You need a start value of zero and the first key of the keys array, not the whole array.
For summing take the accumulator directly.
const arr = [{ a: 1 }, { b: 5 }, { c: 2 }];
const app = (arr) => {
const r = arr.reduce((acc, nextValue) => {
return acc + nextValue[Object.keys(nextValue)[0]];
}, 0);
return r;
};
console.log(app(arr));
Answered by Nina Scholz on November 20, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP