Javascript Array inside Array - how can I call the child array name?
I would create an object like this:
var options = {
size: ["S", "M", "L", "XL", "XXL"],
color: ["Red", "Blue", "Green", "White", "Black"]
};
alert(Object.keys(options));
To access the keys individualy:
for (var key in options) {
alert(key);
}
P.S.: when you create a new array object do not use new Array
use []
instead.
you can get using key
value something like this :
var size = new Array("S", "M", "L", "XL", "XXL");
var color = new Array("Red", "Blue", "Green", "White", "Black");
var options = new Array(size, color);
var len = options.length;
for(var i = 0; i<len; i++)
{
for(var key in options[i])
{
alert(options[i][key])
}
}
see here : http://jsfiddle.net/8hmRk/8/
There is no way to know that the two members of the options
array came from variables named size
and color
.
They are also not necessarily called that exclusively, any variable could also point to that array.
var notSize = size;
console.log(options[0]); // It is `size` or `notSize`?
One thing you can do is use an object there instead...
var options = {
size: size,
color: color
}
Then you could access options.size
or options.color
.