I want to sum up the thinking like the following code:
const bests = [
{
thinking: { _id: '6347d5b4edc3227f056cbc0c', sum: 64, num: 1 },
listening: { _id: '6347d5b4edc3227f056cbc0d', sum: 129, num: 2 },
speaking: { _id: '6347d5b4edc3227f056cbc0e', sum: 0, num: 2 },
writing: { _id: '6347d5b4edc3227f056cbc0f', sum: 0, num: 0 },
speed: { _id: '6347d5b4edc3227f056cbc10', sum: 187, num: 2 },
overall: { _id: '6347d5b4edc3227f056cbc11', sum: 129, num: 2 }
},
{
thinking: { _id: '6347e46665609042876fc4a6', sum: 100, num: 1 },
listening: { _id: '6347e46665609042876fc4a7', sum: 100, num: 1 },
speaking: { _id: '6347e46665609042876fc4a8', sum: 100, num: 1 },
writing: { _id: '6347e46665609042876fc4a9', sum: 0, num: 0 },
speed: { _id: '6347e46665609042876fc4aa', sum: 88, num: 1 },
overall: { _id: '6347e46665609042876fc4ab', sum: 100, num: 1 }
},
{
thinking: { _id: '6347e54f65609042876fc4af', sum: 10, num: 10 },
listening: { _id: '6347e54f65609042876fc4b0', sum: 65, num: 1 },
speaking: { _id: '6347e54f65609042876fc4b1', sum: 0, num: 1 },
writing: { _id: '6347e54f65609042876fc4b2', sum: 0, num: 0 },
speed: { _id: '6347e54f65609042876fc4b3', sum: 95, num: 1 },
overall: { _id: '6347e54f65609042876fc4b4', sum: 65, num: 1 }
}
]
const sum = bests.reduce((a, b) => a['thinking'].sum + b['thinking'].sum);
console.log('sum::', sum);
But as you see I get the wired error!
How can I fix this?
>Solution :
The following code give you the expected results.
const bests = [
{
thinking: { _id: '6347d5b4edc3227f056cbc0c', sum: 64, num: 1 },
listening: { _id: '6347d5b4edc3227f056cbc0d', sum: 129, num: 2 },
speaking: { _id: '6347d5b4edc3227f056cbc0e', sum: 0, num: 2 },
writing: { _id: '6347d5b4edc3227f056cbc0f', sum: 0, num: 0 },
speed: { _id: '6347d5b4edc3227f056cbc10', sum: 187, num: 2 },
overall: { _id: '6347d5b4edc3227f056cbc11', sum: 129, num: 2 }
},
{
thinking: { _id: '6347e46665609042876fc4a6', sum: 100, num: 1 },
listening: { _id: '6347e46665609042876fc4a7', sum: 100, num: 1 },
speaking: { _id: '6347e46665609042876fc4a8', sum: 100, num: 1 },
writing: { _id: '6347e46665609042876fc4a9', sum: 0, num: 0 },
speed: { _id: '6347e46665609042876fc4aa', sum: 88, num: 1 },
overall: { _id: '6347e46665609042876fc4ab', sum: 100, num: 1 }
},
{
thinking: { _id: '6347e54f65609042876fc4af', sum: 10, num: 10 },
listening: { _id: '6347e54f65609042876fc4b0', sum: 65, num: 1 },
speaking: { _id: '6347e54f65609042876fc4b1', sum: 0, num: 1 },
writing: { _id: '6347e54f65609042876fc4b2', sum: 0, num: 0 },
speed: { _id: '6347e54f65609042876fc4b3', sum: 95, num: 1 },
overall: { _id: '6347e54f65609042876fc4b4', sum: 65, num: 1 }
}
]
// changed code block start
const sum = bests.reduce((a, b) => a + b['thinking'].sum,0);
// changed code block end
console.log('sum::', sum);
Explanation : a is the sum of all values before adding current value. Therefore it is a number not an item of this list and calling a['thinking'].sum cause error because a is a number therefore a['thinking'] is undefined. b is the current item of list. Also you have to pass the initial value which is 0.