I want to loop through an object, perform some changes to each of it's properties, and then push those to an array. Each object property is pushed multiple times (in the JSFiddle, I've set it to push twice for simplicity). Each 'iteration' has a few of the properties different (JSFiddle shows only 1, i.e. the 'number').
However, it seems that all objects being pushed in a single loop cannot possess unique properties. I'm looking for a solution for this.
Sorry for my poor English, it's difficult to explain issue and it would be easier to see the JSFiddle.
Actual output:
[{ x: 1, number: 1 },
{ x: 1, number: 1 },
{ y: 2, number: 1 },
{ y: 2, number: 1 },
{ z: 3, number: 1 },
{ z: 3, number: 1 }]
Expected output:
[{ x: 1, number: 0 },
{ x: 1, number: 1 },
{ y: 2, number: 0 },
{ y: 2, number: 1 },
{ z: 3, number: 0 },
{ z: 3, number: 1 }]
Code:
var items = {
"a": {
"x": 1
},
"b": {
"y": 2
},
"c": {
"z" : 3
}
};
var something = [];
for ( var key in items ) {
var item = items[key];
if ( items.hasOwnProperty(key) ) {
for ( var i = 0; i < 2; i++ ) {
item.number = i;
something.push(item);
}
}
}
console.log(something);