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 6890043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:13:55+00:00 2026-05-27T06:13:55+00:00

I have an array of 20 objects BaseObjects called ArrayBaseObjects. The user calls an

  • 0

I have an array of 20 objects BaseObjects called ArrayBaseObjects.
The user calls an object, it’s loaded in the UI, and he makes changes to the properties.
Something like this:

var ArrayBaseObjects = new Array();
var CurrentObject = null;

function OpenRecord (TheIndex) {
  CurrentObject = ArrayBaseObjects[TheIndex];
}

function RecordChanges() {
 // bunch of statements that make changes to CurrenObject
 CurrentObject.CrazyStuff = NewValue;
}

The problem is that when the user makes changes to the CurrentObject, it also changes the value of the ORIGINAL object in ArrayBaseObjects.

I don’t understand why?? When I write CurrentObject = ArrayBaseObjects[TheIndex]; why does changing CurrentObject also affect the value of the objects in ArrayBaseObject??

I’m looking to compare that value between the orignal object and CurrentObject the user made changes to, but they’re always the same! What changes do I need to make to get this to work the way I intend it?

Thanks for the explanation.

  • 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-05-27T06:13:56+00:00Added an answer on May 27, 2026 at 6:13 am

    Pretty much everything in JavaScript is passed around by reference. That means unless you explicitly clone an object, assigning the object to a new variable means that now you have two variables pointing to the same object, and changes made to that object through one variable will be visible from the second.

    For an answer on how to fix this, what you want is to clone the object. However, this isn’t necessarily a trivial thing to perform in an elegant way.

    Fortunately, JavaScript ninjas out there have already done the hard work. I would suggest using jQuery and it’s excellent extend() method, which you can specify to do a deep copy. This makes sure that all the data in the object gets cloned, rather than just the top level references.

    For more information see this question.

    In your case, you would use it like so:

    var ArrayBaseObjects = [];
    var CurrentObject = {};
    
    function OpenRecord (TheIndex) {
      // This will perform a deep copy of all elements of the ArrayBaseObjects[TheIndex]
      // into CurrentObject
      jQuery.extend(true, CurrentObject, ArrayBaseObjects[TheIndex]);
      // You could also call it like this, if you didn't need the CurrentObject variable:
      // var clonedObject = jQuery.extend(true, {}, ArrayBaseObjects[TheIndex]);
    }
    
    function RecordChanges() {
     // bunch of statements that make changes to CurrenObject
     CurrentObject.CrazyStuff = NewValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of objects called objarray . Each object looks like this:
I have a array of objects - something like this: [ {name : blar,
I have a Array of objects which is something like this : SomeObject (Array)
I have an array of objects. The object has two properties a value and
I have an array of objects which populate a UITableView . When a user
I have an array of objects in MATLAB and I've called their constructors in
I have an array of CLLocation objects and I'd like to be able to
I have array of objects with created images (Object.Image), i want to show images
I have an array of objects that have 2 properties: Key and Value. I
Why, if I have array of objects like this: class testClass { private $_x

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.