21

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?

peterh
  • 9,698
  • 15
  • 68
  • 87
Malyo
  • 1,900
  • 6
  • 28
  • 51
  • 5
    Not use new Array, use [] instead. – antonjs Mar 26 '12 at 12:10
  • here the difference btw `[]` and `new Array` http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-ar/1273936#1273936 – antonjs Mar 28 '12 at 13:59

8 Answers8

29

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.

antonjs
  • 13,120
  • 11
  • 61
  • 86
  • It kinda works, but it displays all the names, how do i access them individualy (i want it inside loop afterwards) – Malyo Mar 28 '12 at 07:02
  • 1
    Just execute this code: `for (var key in options) { alert(key); }` – antonjs Mar 28 '12 at 08:17
  • MDN (Mozilla) suggests that for in should not be used on arrays - "for..in should not be used to iterate over an Array where index order is important. Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties." - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in – rpeg May 18 '14 at 02:57
5

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/

Jignesh Rajput
  • 3,480
  • 25
  • 50
  • Well your example doesn't work mate, it alerts all the array array values, not the array array name – Malyo Mar 28 '12 at 07:01
2

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.

alex
  • 438,662
  • 188
  • 837
  • 957
2

You can't. The array doesn't have a name.

You just have two references to the array, one in the variable and another in the third array.

There is no way to find all the references that exist for a given object.

If the name is important, then store it with the data.

var size = { data: ["S", "M", "L", "XL", "XXL"], name: 'size' };
var color = { data: ["Red", "Blue", "Green", "White", "Black"], name: 'color' };
var options = [size, color];

Obviously you'll have to modify the existing code which accesses the data (since you now have options[0].data[0] instead of options[0][0] but you also have options[0].name).

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
  • 'no way to find all the references that exist' - seems incorrect, sorry Quentin. You can loop through properties in an object to see what's there. – dmp Mar 26 '12 at 12:05
  • 1
    @danp — You'd have to loop through *every* object, including those that were wrapped up in closures. Assuming that this code is in a function, I'm not aware of any way to find all the variables declared in it (if it wasn't then `var color` would should up as `window.color`) – Quentin Mar 26 '12 at 12:05
1

Yes it is. You can use

alert(options[0][0])

to get the size "S"

or

alert(options[0][1])

to get the color "Red"

Tobi
  • 1,446
  • 1
  • 13
  • 26
0

Array is just like any other object. You can give it a name if you want.

var size = new Array("S", "M", "L", "XL", "XXL");
var color = new Array("Red", "Blue", "Green", "White", "Black");
var options = new Array( size, color);
color.name = "color";
size.name = "size";
options[0].name == "size"
>true
0

In that case you don't want to insert size and color inside an array, but into an object

var options = { 
    'size': size,
    'color': color
  };

Afterwards you can access the sets of keys by

var keys = Object.keys( options );
Sirko
  • 65,767
  • 19
  • 135
  • 167
0

You've made an array of arrays (multidimensional), so options[0] in this case is the size array. you need to reference the first element of the child, which for you is: options[0][0].

If you wanted to loop through all entries you can use the for .. in ... syntax which is described here.

var a = [1,2,4,5,120,12];
for (var val in t) {
    console.log(t[val]);
}

var b = ['S','M','L'];
var both = [a,b];

for (var val in both) {
     for(val2 in both[val]){console.log(both[val][val2])}
}
dmp
  • 12,837
  • 6
  • 39
  • 46