-1

I need to match the name of the array with the name of a nesting array inside. Y has many, many nesting arrays. I gave two as an example. Also, i always has a random nesting array name. In this example, i is equal to car.

var y = {car:['honda','ford'],color:['red','green']/*,...More here*/};
    i = 'car'; //This value can change to either 'car', or 'color', etc...
    var x = y + i;
    console.log(x);

I need to get the value of x to become y.car, which will log ["honda", "ford"]. Instead, x logs [object Object]car. How do I get x, var x = y + i, to return ["honda", "ford"] instead?

EDIT:

Yes, but x = y[i] is not working in my for loop.


    for (i = 0; y.length > i; i++) {
        console.log(y[i]);
      }

Ackados
  • 95
  • 1
  • 9

1 Answers1

1

Try this:

var y = {car:['honda','ford'],color:['red','green']/*,...More here*/};
var keys = Object.keys(y);
for (var i = 0; i < keys.length; i++) {
    console.log(y[keys[i]]);
}
JakeBoggs
  • 274
  • 4
  • 17