0

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.

  • `object_list.push('car'+z);` means, you are pushing the strings of "car1", "car2" and "car3" to the array – wangdev87 Dec 01 '20 at 19:25
  • One option: `var object_list = [{type:"Fiat", model:"500", color:"white"}, {type:"Ford", model:"Focus", color:"red"}, {type:"Nissan", model:"Micra", color:"Blue"}];` – 001 Dec 01 '20 at 19:26
  • It doesn't work because you're just creating strings, that's NOT variable names. The same way when you do `var hello = "world"` only `hello` is a variable, `"world"` is its string value. See [“Variable” variables in Javascript?](https://stackoverflow.com/q/5187530) for more on what you're attempting to do. The preferred approach is to create an array of your objects or at the very least an object, so you'd be able to dynamically generate the keys. However, in your case, I don't see the reason not to directly hardcode the array with the cars in it. – VLAZ Dec 01 '20 at 19:26
  • What if my objects were built from a constructor ? Would i still be able to put them all into one large array? – smartiedude Dec 01 '20 at 19:47
  • @smartiedude of course, why not? Anything you can assign to a variable can be put in an array literal: `[1, "hello", {type:"Fiat", model:"500", color:"white"}, new Car("ford", "focus", "red"), ["another", "array], evenAVariable ]`, and so on. – VLAZ Dec 01 '20 at 19:53
  • Okay brilliant. I'll do this then. Thank you to everyone for the input. Much appreciated. – smartiedude Dec 01 '20 at 20:05

1 Answers1

0

You can wrap the variable in window['car1'] and it will create the dynamic variable name. Its not the optimal solution but it will accomplish what you are going for.

The optimal solution would be to create one large array instead.

// 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(window['car'+z]);
}

console.log(object_list[2].type)  //undefined
imvain2
  • 15,480
  • 1
  • 16
  • 21