Javascript Find and Push Items into Array
<p>I have a dataset being returned from an AJAX call that contains a list of users and their role in association to the project.</p>

<p>A user can hold multiple roles and therefore be in the result set in various places.</p>

<p>I am trying to create an Array/Object that I can push the user into the first time along with their role and then any time it sees that user again, it just adds their role to their existing array.</p>

<p>For example, and this is way off I'm sure but its the concept..</p>

<pre><code>var tempRoles = [];

// Loop over our users. If they are already in the array, just add their additional row.
$($response.associatedUsers.users.user).each(function(){
   tempRoles.push({
     QID: this.QID,
     role: this.Role
  })
});
</code></pre>

<p>In the end, I need to have a list of all the individual users and then an array of all their roles that they hold. </p>

<p>This is what I would think the array would look like</p>

<pre><code>Users &lt;-- Object?
  Bob123 &lt;-- User Name
   Roles &lt;-- Array
     Admin &lt;-- Role
     Mod &lt;-- Role
     Super Mod &lt;-- Role
  Jimmy456
   Roles
    Admin
    Mod
</code></pre>

<p>The only catch to this is I need to in a format where I can look up the user directly and see their roles.</p>

<pre><code>users['Bob123'].roles
</code></pre>

<p>I know my example isn't that helpful but hopefully it shows what I am trying to accomplish here.</p>

<p><strong>Update:</strong>
This is what the object looks like right now, printed to the console. As you can see, the same ID is in there multiple times with different roles. </p>

<p>I need to be able to have a single user and their multiple roles that they hold.
<a href="https://i.stack.imgur.com/5OYUA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5OYUA.png" alt="enter image description here"></a></p>

<p>Not sure the correct formatting on this but its how I would assume the output would be:</p>

<pre><code>users [
 Bob123[
  roles [
    'admin', 'mod', 'superMod'
  ],
  Jimmy456[
   roles [
     'admin', 'mod'
   ]
 ]
]
</code></pre>