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

前端 未结 8 914
不思量自难忘°
不思量自难忘° 2020-12-24 13:42

Here is the example of what I am doing:

   var size = new Array("S", "M", "L", "XL", "XXL");
   var color =          


        
相关标签:
8条回答
  • 2020-12-24 14:19

    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.

    0 讨论(0)
  • 2020-12-24 14:20

    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 );
    
    0 讨论(0)
  • 2020-12-24 14:23

    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"

    0 讨论(0)
  • 2020-12-24 14:28

    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])}
    }
    
    0 讨论(0)
  • 2020-12-24 14:29

    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).

    0 讨论(0)
  • 2020-12-24 14:30

    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/

    0 讨论(0)
提交回复
热议问题