I have a question for this case
Input:
data = [
{ number_1: 10, number_2: 20 },
{ number_1: 15, number_2: 5 },
{ number_1: 15, number_2: 35 },
];
Output:
dataSum = { sum_number_1: 40, sum_number_2: 60 };
I want to ask for 2 method we can use: array.reduce and array.forEach. And I see we need to transform array to an object for output. Thanks for your help.
reducefor this, it overcomplicates things. (In general, if you're not doing functional programming with predefined, reusable reducer functions, don't usereduce.) You'll seereduceused a lot, almost always where the code would be simpler and clearer with a loop. I also wouldn't useforEach, I'd use afor-ofloop, butforEachcan work as well.0. Loop through the array adding the property values from each object to the values in your result object.