2
 var p = {
     id: null
 };
 for (var copyArray = [], i = 0; i < 3; i++) {
     copyArray.push(p);
     copyArray[i].id = (copyArray.length) - parseInt(1, 10);
 }
 console.log(copyArray);

All id in copyArray is getting 2 value. Result CopyArray({id=2},{id=2},{id=2})

Doing normal push operation of object in array, and updating the index after insertion.

But somehow all id's in the copy array are getting same id What wrong i am doing over here

6
  • 3
    Side note: parseInt(1, 10) is just a long-winded and inefficient way to write 1. parseInt is for parsing strings as integers. Commented Aug 24, 2015 at 6:06
  • @T.J.Crowder what is 10 in parseInt(1,10)? Commented Aug 24, 2015 at 6:07
  • 1
    @Alex Converting to Decimal, you can also use 2, 8, 16 for binary, octal, hex respectively Commented Aug 24, 2015 at 6:07
  • 1
    @Alex: The radix (number base) to use. 10 (decimal) is the number base we normally write numbers in. It's 10 because each "place" in a number is a power of 10. E.g., 123 is (1 x 10²) + (2 x 10¹) + (3 x 10⁰). Commented Aug 24, 2015 at 6:07
  • 1
    @Alex: I didn't use any one resource, just a lot of things I've picked up over the years. My two primary references are the specification and MDN. MDN is more user-friendly, but is also collaboratively-edited by anyone who wants to sign up for a Mozilla account. It used to be consistently excellent; the last couple of years people have been contributing who aren't quite as rigorous and well-informed as the original authors, Still very good. Do not use w3schools. Commented Aug 24, 2015 at 6:15

2 Answers 2

9

You're pushing the same object into the array repeatedly, and just updating the id property on that object as you go.

If you want multiple objects in the array, you'll need to create multiple objects:

var copyArray = [];
while (copyArray.length < 3) {
  copyArray.push({
    id: copyArray.length
  });
}
snippet.log(JSON.stringify(copyArray));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

It worked thanks.. Is there any explanation why we can't do that way,,?
@KunalVashist: The first sentence is the explanation: You're pushing the same object repeatedly.
-1

You can also create a function that will create clone for the Object:

JSON.clone = function(json) {
    var
        temp = null,
        key = null;

    if (json === null || typeof json !== 'object') {
        return json;
    }
    temp = json.constructor(); // changed
    for (key in json) {
        if (json.hasOwnProperty(key)) {
            temp[key] = JSON.clone(json[key]);
        }
    }
    return temp;
};

Then you can use your original code but with little changes:

var p = {
    id: null
};
for (var copyArray = [], i = 0; i < 3; i++) {
    copyArray.push(JSON.clone(p));
    copyArray[i].id = copyArray.length - 1;
}
console.log(copyArray);

1 Comment

why down-vote this answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.