I created some objects which I wanted to push into an array. Instead of pushing them all manually, I wondered if it might be less code re-use if I pushed via a FOR loop. However, it's not working.
// Using a FOR to push objects into an array.
var object_list = [];
var car1 = {type:"Fiat", model:"500", color:"white"};
var car2 = {type:"Ford", model:"Focus", color:"red"};
var car3 = {type:"Nissan", model:"Micra", color:"Blue"};
//object_list.push(car1); ..the manual way..
//object_list.push(car2);
//object_list.push(car3);
for (z = 1; z < 4; z++) { // .. a better way.. or so I thought!..
object_list.push('car'+z);
}
alert(object_list[2].type) //undefined
Can anyone help tell me why the FOR loop doesn't work?.. or if there's another way.