Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8117577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:13:10+00:00 2026-06-06T04:13:10+00:00

So I have an object, that I’m using in nodejs. It looks as such:

  • 0

So I have an object, that I’m using in nodejs. It looks as such:

for(var i = 0; i < x.length; i++) {
            var sUser = x[i];
            mUsers[sUser.userid] = CreateUser(sUser);
            ++mUsers.length;
        }

So I’m pulling information from an external source, and it breaks down as an array full of instances of this:

[{ name: 'Michael Lovesllamas Lankford',
 created: 1338420951.11,
 laptop: 'pc',
 laptop_version: null,
 userid: '4fc6aed7eb35c14ad6000057',
 acl: 0,
 fans: 1,
 points: 5,
 avatarid: 34 }]

and so forth.

so that information is passed as x in the above function.

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 > 0) b.isSuperUser = true;
    return b;
};

global.RegisterUsers = function(x) {
        for(var i = 0; i < x.length; i++) {
            var sUser = x[i];
            mUsers[sUser.userid] = sUser;
            ++mUsers.length;
        }

}

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:

{ 
 userid1: { userid: userid3, name: name3, item: item3 },
 userid2: { userid: userid3, name: name3, item: item3 },
 userid3: { userid: userid3, name: name3, item: item3 }
}

And I don’t know why it’s overwriting. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T04:13:12+00:00Added an answer on June 6, 2026 at 4:13 am

    The main problem is that you where continuously referencing the same object when you where calling CreateUser, as such it was simply updating and returning a reference which was being kept through out all the calls, this is why when you where printing it, it just printed the last update.
    You need to create a copy of the object.

    global.CreateUser = function(a) {
        var b = Object.create(UserBase); // this will create a copy of it.
        b.userid = a.userid;
        b.name = a.name;
        b.laptop = a.laptop;
        if (a.acl > 0) b.isSuperUser = true;
        return b;
    };
    

    now CreateUser is actually creating a copy, when you go through the properties the default ones may not appear right away, but theres still there, they’ve being simply moved to __proto__ you can still call them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object that looks like var obj = { a: { a:
I have JSON Object that looks something like the below object, this object can
I have an object that looks something like names = { m: [ 'adam',
I have an object that has a list of abstract 'aninamls'. i.e. var animals
I have an object that reads data from an Excel file using, which takes
I have an object model that looks like this: public MyObjectInJson { public long
I have an object that contains another object with translations. It looks like this:
I have an object that looks like this: class Model { public string Category
I have an object that looks like this { AF : { name :
I have an object that I convert into NSData using an NSKeyedArchiver and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.