Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my JS Loop overwriting previous entries in my object?
    primarykey
    data
    text
    <p>So I have an object, that I'm using in nodejs. It looks as such:</p> <pre><code>for(var i = 0; i &lt; x.length; i++) { var sUser = x[i]; mUsers[sUser.userid] = CreateUser(sUser); ++mUsers.length; } </code></pre> <p>So I'm pulling information from an external source, and it breaks down as an array full of instances of this:</p> <pre><code>[{ name: 'Michael Lovesllamas Lankford', created: 1338420951.11, laptop: 'pc', laptop_version: null, userid: '4fc6aed7eb35c14ad6000057', acl: 0, fans: 1, points: 5, avatarid: 34 }] </code></pre> <p>and so forth.</p> <p>so that information is passed as x in the above function.</p> <pre><code>global.mUsers = {length:0} global.UserBase = { userid: -1, name: "noidea", isSuperUser: false, isDJ: false, laptop: "pc" }; process.on("registered", OnRegistered); global.OnRegistered = function(a) { //misc code here RegisterUsers(a.users); //misc code here } global.CreateUser = function(a) { var b = UserBase; b.userid = a.userid; b.name = a.name; b.laptop = a.laptop; if (a.acl &gt; 0) b.isSuperUser = true; return b; }; global.RegisterUsers = function(x) { for(var i = 0; i &lt; x.length; i++) { var sUser = x[i]; mUsers[sUser.userid] = sUser; ++mUsers.length; } </code></pre> <p>}</p> <p>Now, I've logged it in the loop, and mUsers[sUser.userid] does indeed = sUser. but when I console.log(mUsers) immediately after the loop, I get this:</p> <pre><code>{ userid1: { userid: userid3, name: name3, item: item3 }, userid2: { userid: userid3, name: name3, item: item3 }, userid3: { userid: userid3, name: name3, item: item3 } } </code></pre> <p>And I don't know why it's overwriting. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload