-1

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?

3
  • 1
    a is the accumulator, it doesn't have a thinking property, you should use something like a + b.thinking.sum but in order for this to work, you need to set a starting value for the accumulator. You can do that like this: bests.reduce((a, b) => a + b.thinking.sum, 0); Commented Oct 13, 2022 at 13:18
  • const sum = bests.map(a => a.thinking.sum).reduce((a, b) => a + b); ? Commented Oct 13, 2022 at 13:20
  • Also related, if not a dupe: stackoverflow.com/q/66825357/215552 or stackoverflow.com/q/33392307/215552 Commented Oct 13, 2022 at 13:23

2 Answers 2

2

The first param of reduce() is the accumulator, no need for ['thinking] on that:

Pass 0 as the initial value, so the accumulator becomes an integer, THen we can just add the b['thinking'].sum:

bests.reduce((a, b) => a + b['thinking'].sum, 0);

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 + b['thinking'].sum, 0);
console.log('sum::', sum);

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

Please add some details to your answer. "change the code as above" does not help OP. Explain what you did, and why!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.