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

前端 未结 8 915
不思量自难忘°
不思量自难忘° 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:36

    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.

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

    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 讨论(0)
提交回复
热议问题