Need help with figuring out how to get the sum of multiple arrays.
This is my Data outputting from the console.log:
(4) [empty, Array(1), Array(1), Array(4)]
1: [50, 30]
2: [100, 100, 50]
3: (4) [500, 10, 50, 300]
Expected output:
1: [80]
2: [250]
3: [860]
Methods used:
This code below doesn't work, it's just the columns of each row
const savingsTotal = saveID[0].map((x, idx) => saveID.reduce((sum, curr) => sum + curr[idx], 0));
code source : How to calculate the sum of multiple arrays?
This code gives me a weird string:
const savingsTotal = saveID.reduce((sum, value) => sum += value, 0)
Output: 050100500,10,50,300
Thought if I list with a key that would work, eg [saved: 500], but it didn't up working either.
const savingsTotal = saveID.map(({saved}) => saved.reduce((sum, value) => sum += value, 0))
Output: breaks code, can't find [saved : 500] due to the previous function which is:
if (save.length > 0 ) {
const saveID = save.reduce((savedAmount, { goal_id, saved }) => {
(savedAmount[goal_id] = savedAmount[goal_id] || []).push({"saved": saved});
return savedAmount;
}, []); ****Changed the .push so I would be able to add a key, normally it's without.
code source: How to get a the sum of multiple arrays within an array of objects?
Can someone help me out with the logic?