reduce method takes 2 important args: your reducing function and the default value at step 0 of the iteration
Here the reducer has 2 args:
- the so-called
previousValue: here called reducerTarget as it receives what is returned in the body of the reducer
- the
currentValue where the reducer is: here called currentArrayItem
Here's what you might want
const {totalLCI, totalMCI} = data.reduce((reducerTarget, currentArrayItem) => {
return {
totalLCI: reducerTarget.totalLCI + currentArrayItem.option.lci,
totalMCI: reducerTarget.totalMCI + currentArrayItem.option.mci
}
}, {totalLCI: 0, totalMCI: 0})
From here, starting with the default value, what it does on the first iteration is
/* 1st step:
index = 0
targetReducer = defaultValue = {totalMCI: 0, totalLCI: 0}
currentArrayItem = {option: {lci: 1, mci: 2}}
*/
return {
totalLCI: 0 + 1,
totalMCI: 0 + 2
}
/* 2nd step:
index = 1
targetReducer = previousValue = {totalMCI: 1, totalLCI: 2}
currentArrayItem = {option: {lci: 3, mci: 4}}
*/
return {
totalLCI: 1 + 3,
totalMCI: 2 + 4
}