阵内的JavaScript数组 - 我怎么能叫孩子数组名 [英] Javascript Array inside Array - how can i call child array name

查看:104
本文介绍了阵内的JavaScript数组 - 我怎么能叫孩子数组名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在做什么的例子:

 变种大小=新阵列(S,M,L,XL,超大);
   VAR颜色=新的Array(红,蓝,绿,白,黑);
   VAR的选择=新阵列(大小,颜色);

我在做一个循环选择表格一样的东西它的工作好,但我想获取数组孩子的名字,在这种情况下 - 大小或颜色。当我做警报(选项[0])我得到阵列的所有元素,但对于一些特定的情况下,我想只有数组名,这就像我已经说过的大小/颜色。有没有办法做到这一点?谢谢


解决方案

我想创建一个对象是这样的:

  VAR选项= {
    大小:[S,M,L,XL,XXL],
    颜色:红,蓝,绿,白,黑]
};
警报(Object.keys(选项));

要individualy访问键:

 为(以选择VAR键){
    警报(键);
}

PS:当你创建一个新的数组对象不使用新阵列使用 [] 而不是

Here's the example of what i'm 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'm 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'm 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've said already. Is there way to achieve that? Thanks

解决方案

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.

这篇关于阵内的JavaScript数组 - 我怎么能叫孩子数组名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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