title: How can I sum the sum of a property of an javascript array in an array of objects?
body: <p>I'm making a game where each player can submit an answer on a given question and other players can vote on an answer of another player. I store these results in an array of game rounds which may look something like the following:</p>
  <pre class="lang-js prettyprint-override"><code>const roundHistory = [
    {
      question: 'question1',
      submissions: [
        {
          explanation: 'answer1',
          player: { id: 'id1', name: 'player1' },
          votes: [
            { id: 'id2', name: 'player2' },
            { id: 'id3', name: 'player3' },
          ],
        },
        {
          explanation: 'answer2',
          player: { id: 'id2', name: 'player2' },
          votes: [{ id: 'id1', name: 'player1' }],
        },
        {
          explanation: 'answer3',
          player: { id: 'id3', name: 'player3' },
          votes: [],
        },
      ],
    },
    {
      question: 'question2',
      submissions: [
        {
          explanation: 'answer1',
          player: { id: 'id1', name: 'player1' },
          votes: [
            { id: 'id2', name: 'player2' },
            { id: 'id3', name: 'player3' },
          ],
        },
        {
          explanation: 'answer2',
          player: { id: 'id2', name: 'player2' },
          votes: [{ id: 'id1', name: 'player1' }],
        },
        {
          explanation: 'answer3',
          player: { id: 'id3', name: 'player3' },
          votes: [],
        },
      ],
    },
  ];
  
  </code></pre>
  <p>As you can see there are 3 players in the game since each round (index of the roundHistory array) has 3 submissions. Each submission has a property <code>player</code> which represents the player who submitted the submission. And then each submission has a property <code>votes</code> which is an array of the players who voted for that submission. Now my question:</p>
  <p><strong>How can I get the total received votes per player of all rounds?</strong></p>
  <hr/>
  <p>What I've tried so far... I thought, first I'd get the total received votes per player per round like so:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const roundHistory = [{question:'question1',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},{question:'question2',submissions:[{explanation:'answer1',player:{id:'id1',name:'player1'},votes:[{id:'id2',name:'player2'},{id:'id3',name:'player3'},],},{explanation:'answer2',player:{id:'id2',name:'player2'},votes:[{id:'id1',name:'player1'}],},{explanation:'answer3',player:{id:'id3',name:'player3'},votes:[],},],},];
  
  const getTotalReceivedVotesPerPlayerInRound = (round) =&gt; {
    return roundHistory[round].submissions.map((s) =&gt; ({
      player: s.player,
      totalVotes: s.votes.length,
    }));
  }
  
  console.log(getTotalReceivedVotesPerPlayerInRound(0));</code></pre>
  </div>
  </div>
  </p>
  <p>But I got stuck at adding all results of all rounds together. Note that I tried to simplify all used objects such as a player. In my application these are mostly classes for which I have several utility methods. One of them is the <code>getTotalReceivedVotesPerPlayerInRound()</code> method that I can use on a round (index of the roundHistory array).</p>
  <p>I expect the final result to be of the same shape as the result of my already written function: An array of objects with two properties: <code>player</code> (the player object) and <code>totalVotes</code>. E. g.:</p>
  <pre><code>const result = [
    {
      &quot;player&quot;: {
        &quot;id&quot;: &quot;id1&quot;,
        &quot;name&quot;: &quot;player1&quot;
      },
      &quot;totalVotes&quot;: 2
    },
    {
      &quot;player&quot;: {
        &quot;id&quot;: &quot;id2&quot;,
        &quot;name&quot;: &quot;player2&quot;
      },
      &quot;totalVotes&quot;: 1
    },
    {
      &quot;player&quot;: {
        &quot;id&quot;: &quot;id3&quot;,
        &quot;name&quot;: &quot;player3&quot;
      },
      &quot;totalVotes&quot;: 0
    }
  ];
  </code></pre>