After experimenting with this code on jsfiddle, I am puzzled as to why it doesn't generate an array of 5 objects, each with a different id property:
var arr = ["1", "2", "3", "4", "5"];
var clone = {"id": "0", "name":"Matthew"};
var arrObj = [];
var idArr = [];
while((a=arr.pop()) != null){
clone.id = a;
console.log(clone);
arrObj.push(clone);
}
console.log(arrObj);
What actually appears in the console is:
Object {id: "5", name: "Matthew"} (index):28
Object {id: "4", name: "Matthew"} (index):28
Object {id: "3", name: "Matthew"} (index):28
Object {id: "2", name: "Matthew"} (index):28
Object {id: "1", name: "Matthew"} (index):28
[Object, Object, Object, Object, Object]
Upon inspection, all 5 cloned objects have an "id" value of "1."
The question remains: Why does this happen?