title: Normalise array of objects & key by id
body: <p>I'm looking to normalise some data returned from an API. Already using underscore within the project. </p>
  
  <pre class="lang-js prettyprint-override"><code>var res = [{
      badge_no: 123,
      id: 1,
      name: 'bob'
    }, {
      badge_no: 456,
      id: 2,
      name: 'bill'
    }, {
      badge_no: 789,
      id: 3,
      name: 'ben'
    },
    // etc
  ];
  </code></pre>
  
  <p>I'm looking to create a data structure that looks like: </p>
  
  <pre class="lang-js prettyprint-override"><code>var normalisedRes = [{
    1: {
      badge_no: 123,
      id: 1,
      name: 'bob'
    }
  }, {
    2: {
      badge_no: 456,
      id: 2,
      name: 'bill'
    }
  }, {
    3: {
      badge_no: 789,
      id: 3,
      name: 'ben'
    }
  }];
  </code></pre>
  
  <p>It is important that I keep the id within the obj. I believe I can accomplish this with <code>reduce</code> but I'm struggling.</p>
  
  <p>Thanks for your help!</p>
  
  <p>EDIT: This question has now been answered. </p>
  
  <p>From some of the advice on here, I have decided to normalise the data to return an obj which looks like:</p>
  
  <pre><code>{ '1': {data}, '2':{data}, '3':{data} }
  </code></pre>
  
  <p>To do this I used reduce, as I originally thought I should:</p>
  
  <p><code>var normalised = res.reduce((acc, person) =&gt; {
    acc[person.id] = person;
    return acc;
  }, {});</code></p>
  
  <p>Thanks again for all the answers!</p>