数组内的Javascript数组-如何调用子数组名称? [英] Javascript Array inside Array - how can I call the child array name?

查看:68
本文介绍了数组内的Javascript数组-如何调用子数组名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在做的事的例子:

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

我正在做一个循环选择形式的事情,效果很好,但是在这种情况下,我想获取Array子名称-大小或颜色.当我做alert(options [0])时,我得到了数组的整个元素.但是对于某些特定情况,我只想获取数组名,就像我已经说过的那样,它是大小/颜色.有办法实现吗?

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?

推荐答案

我会创建一个像这样的对象:

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

要单独访问密钥:

for (var key in options) {
    alert(key);
}

P.S .:创建新数组对象时,请勿使用 new Array ,而应使用 [] .

P.S.: when you create a new array object do not use new Array use [] instead.

这篇关于数组内的Javascript数组-如何调用子数组名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆