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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:46:46+00:00 2026-06-02T04:46:46+00:00

I have a complex javascript object with an array. When I try to set

  • 0

I have a complex javascript object with an array. When I try to set the value for one attribute of an index, it’s applied to all items of the array.

Here is a basic example:

var obj = new Object();    
obj.arr = [];
obj.arr[0] = {pos:[0,0]};
obj.arr[1] = {pos:[0,0]};

Now if i set a value for an attribute of the object, via a specific index,

obj.arr[0].pos = [10,10];
obj.arr[1].pos = [5,5];

Here it seems to be setting the value [5,5] for both items of the array. The resulting values are:

console.log(obj.arr[0].pos) returns [5,5]
and

console.log(obj.arr[1].pos) also returns [5,5]

My actual object is far more complex, but this is the basic idea of what’s happening…

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-02T04:46:47+00:00Added an answer on June 2, 2026 at 4:46 am

    They share the same link, i.e. several variables/properties of an object are referring to the same value.

    Exact answer (where’s the error) depends on how your object is composed.

    var nested = {a:1};
    var obj = {arr:[]};
    obj.arr[0] = {pos:0, n:nested};
    obj.arr[1] = {pos:0, n:nested};
    
    obj.arr[0].pos = 1;
    obj.arr[1].pos == 1; // false
    obj.arr[0].nested.a = 2;
    obj.arr[1].nested.a == 2; // true
    

    Assignment of the same array/object literals is not the same.

    var a = [0];
    var b = [0];
    b[0] = 1;
    a[0] == 1; // false
    b = a;
    a[0] = 2;
    b[0] == 2; // true
    
    a = b = [0];
    a[0] = 1;
    b[0] == 1; // true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have javascript object which consists of complex array of structures with nested structures
I have a complex object graph to represent operation scheduling. At one point, I
I have a javascript variable which is referencing a complex object (it is a
Suppose I have an object foo in my JavaScript code. foo is a complex
I have a complex object which has references to other objects, I am trying
Is there a way to turn a form into a complex JavaScript object based
I have a javascript object: data = { color: red, day: monday, list: {1,2,3,4,5,6}}
I have a fairly complex object which has some C# code written to render
In my web app, I have some complex objects written with JavaScript (ie nested
I've got a somewhat complex JavaScript scrolling view implemented, all contained in a DIV.

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.