I'm trying to get some math on an array of objects. Using the map for getting the value inside of the object and then reduce for made the calcules. For some reason reduce not return the correct value.
This is an example:
var test = [{
id: 1,
value: 2
}];
var newe = test.map((x) => x.value).reduce(function(total, sum) {
console.log([total, sum]);
return total + (sum * 2);
});
It's supposed to return 4, however, it returns 2.
Do you have an idea what is wrong? I share the code on jsfiddle https://jsfiddle.net/dleo7/ckgnrubh/10/
reducean initial value, so it doesn't iterate at all and only resolves to the value of the onevalue.initialValueis provided, or ifinitialValueis provided but the array is empty, the solo value will be returned without callingcallbackFn."