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

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

提交回复
热议问题