title: Reorganizing an Array of Objects in Javascript: "Uncaught TypeError: Cannot read property 'push' of undefined"
body: <p>I have an array of objects, each of which pairs an entry id with a tag id. I'm trying to reorganize this so that I get an object with a single index for each unique <code>entry_id</code> with a corresponding list of the associated tags.</p>
  
  <p><strong>Changing this:</strong></p>
  
  <pre><code>tags_entries = [
            {'entry_id': 1, 'tag_id': 1},
            {'entry_id': 1, 'tag_id': 2},
            {'entry_id': 2, 'tag_id': 1},
            {'entry_id': 2, 'tag_id': 3},
            {'entry_id': 3, 'tag_id': 1}
  ]
  </code></pre>
  
  <p><strong>To This:</strong></p>
  
  <pre><code>   entries = {         
           1:
           { 
            'tags': [1, 2]
           },
           2:
           {
            'tags': [1, 3]
           },
           3:
           {
            'tags': [1]
           }
      }
  </code></pre>
  
  <p>The function I have so far is below, but I'm getting this error with it: <code>Uncaught TypeError: Cannot read property 'push' of undefined</code>, which is coming from the line after the <code>else</code></p>
  
  <pre><code>function collect_tags(tags_entries) {
      out = {};
  
      for (i=0; i&lt;tags_entries.length;i++) 
      {
          out[tags_entries[i]['entry_id']] = {};
  
          if (!out.hasOwnProperty(tags_entries[i]['entry_id']))
          {
  
              out[tags_entries[i]['entry_id']]['tags'] = [];
              out[tags_entries[i]['entry_id']]['tags'] = tags_entries[i]['tag_id'];
          } 
          else 
          {
              out[tags_entries[i]['entry_id']]['tags'].push(tags_entries[i]['tag_id']);
          }
      }
  
      return out;
  
  }
  </code></pre>
  
  <p>Can anyone help me figure out what's causing this? Thanks so much.</p>