Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Array inside Array - how can I call the child array name?

Tags:

Here is the example of what I am doing:

   var size = new Array("S", "M", "L", "XL", "XXL");    var color = new Array("Red", "Blue", "Green", "White", "Black");    var options = new Array( size, color); 

I am doing a loop select form thingies which work good, but I want to fetch the Array child name, in this case - size or color. When I am doing alert(options[0]), I get the whole elements of array. But for some specific case, I want to get only the array name, which is size/color like I have said already. Is there way to achieve that?

like image 987
Malyo Avatar asked Mar 26 '12 11:03

Malyo


People also ask

How do you call an array inside an array?

To access the elements of the inner arrays, you simply use two sets of square brackets. For example, pets[1][2] accesses the 3rd element of the array inside the 2nd element of the pets array. If you're used to a language like C, you may be wondering if you can create multi-dimensional arrays in JavaScript.

Can we use array inside array?

Nested Array in JavaScript is defined as Array (Outer array) within another array (inner array). An Array can have one or more inner Arrays. These nested array (inner arrays) are under the scope of outer array means we can access these inner array elements based on outer array object name.

Can we have array inside array in JavaScript?

An array is an ordered collection of values: each value is called an element, and each element has a numeric position in the array, known as its index. JavaScript lets us create arrays inside array called Nested Arrays.

What do you call the things inside an array?

Each variable or object in an array is called an element. Unlike stricter languages, such as Java, you can store a mixture of data types in a single array. For example, you could have array with the following four elements: an integer, a window object, a string and a button object.


Video Answer


1 Answers

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.

like image 113
antonjs Avatar answered Sep 22 '22 20:09

antonjs