0

I've got the following code:

$(document).ready(function(){
    var array = [{"x": 0}];
    array[0] = {"x": 10};
    console.log(array);
    array[0] = {"x": 20};
    console.log(array);
});

With this I expect the output in the console to be:

[
    {
        0: {"x": 10}
    }
]

[
    {
        0: {"x": 20}
    }
]

But instead it is:

[
    {
        0: {"x": 20}
    }
]

[
    {
        0: {"x": 20}
    }
]

Does anyone have any idea why? Am I missing something?

JSFiddle: https://jsfiddle.net/6nfdmt91/

L Ja
  • 1,384
  • 1
  • 11
  • 22

1 Answers1

-5

try:

$(document).ready(function(){
 var array = [{"x": 0}];
 array[0] = {"x": 10};
 console.log(array);
 array[0].push({"x": 20});
 console.log(array);
});

Using Array.push() will "push" a new element.

Daniel
  • 1
  • 1