变量作为关联数组中的索引 - Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4091257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
variable as index in an associative array - Javascript
提问by bogdan
I'm trying to create an associative array, create an empty array, and then add a (indexName
-> value) pair:
我正在尝试创建一个关联数组,创建一个空数组,然后添加一个 ( indexName
-> value) 对:
var arrayName = new Array;
arrayName["indexName"] = value;
// i know i can also do the last line like this:
arrayName.indexName = value;
When I assign the value to the indexName
I want indexName
to be dynamic and the value of a variable. So I tried this:
当我将值分配给indexName
I want indexName
to be dynamic 和变量的值时。所以我试过这个:
arrayName[eval("nume")] = value;
Where:
在哪里:
var var1 = "index";
var var2 = "Name";
var nume = '"' + var1 + var2 + '"';
but: alert(arrayName["indexName"]);
doesn't return "value"... it says "undefined"
但是:alert(arrayName["indexName"]);
不返回“值”......它说“未定义”
Is there something I'm missing? (I'm not familiar with eval()
); if the way I'm trying is a dead end, is there another way to make the index name of the associative array value dynamic?
有什么我想念的吗?(我不熟悉eval()
);如果我尝试的方式是死胡同,是否有另一种方法可以使关联数组值的索引名称动态化?
采纳答案by CMS
At first I don't think you need a real array object to do what you need, you can use a plain object.
起初我认为你不需要一个真正的数组对象来做你需要的,你可以使用一个普通对象。
You can simply use the bracket notation to access a property, using the value of a variable:
您可以简单地使用括号表示法访问属性,使用变量的值:
var obj = {};
var nume = var1 + var2;
obj[nume] = value;
Array's are simply objects, the concept of an "associative array" can be achieved by using a simple object, objects are collections of properties that contain values.
数组只是对象,“关联数组”的概念可以通过使用简单对象来实现,对象是包含值的属性的集合。
True arrays are useful when you need to store numeric indexes, they automatically update their length
property when you assign an index or you push
a value to it.
当您需要存储数字索引时,真正的数组很有用,length
当您为其分配索引或push
值时,它们会自动更新其属性。
回答by balu
You would use objects to do that:
你会使用对象来做到这一点:
var hash = {}
hash["foo"] = "foo";
hash.bar = "bar";
// This is the dynamic approach: Your key is a string:
baz = "baz"
hash[baz] = "hello";
To iterate, just use a for
loop or $.each()
in jQuery.
要迭代,只需使用for
循环或$.each()
在 jQuery 中。
回答by Gabi Purcaru
use arrayName[var1+var2]
用 arrayName[var1+var2]
Note that arrayName.var
is the same as arrayName["var"]
-- it's just syntactic sugar. The second form is mostly used when dealing with the kind of problems that you are facing - dynamic object keys, and keys that are not alphanumeric (think of arrayName[".eval()"]
; this is a perfectly legal object key that has nothing to do with the javascript eval()
function)
请注意,arrayName.var
这与arrayName["var"]
- 它只是语法糖相同。第二种形式主要用于处理您面临的那种问题 - 动态对象键和非字母数字键(想想arrayName[".eval()"]
;这是一个完全合法的对象键,与 javascripteval()
函数无关)
回答by Nick Craver
You want a plain object with the same bracket notaitonhere, like this:
你想要一个带有相同括号 notaiton的普通对象,如下所示:
var arrayName = {};
arrayName["indexName"] = value;
回答by meder omuraliev
Are you looking for variableName = 'bla'+'foo'; arrayRef[variableName] = 'something';
?
你在找variableName = 'bla'+'foo'; arrayRef[variableName] = 'something';
吗?
And even so, you should use an object literal instead. x = {}; x[variablename] = 'blah';
即便如此,您也应该改用对象字面量。 x = {}; x[variablename] = 'blah';
回答by bogdan
Indeed, there was no need for an array object, a simple object did the job; further more an array introduced the need to use quotes inside the square brackets obj["var1 + var2"]
to access the object property's value and not the value associated with an index (i think); the quotes transformed "var1 + var2"
into a string. Using a simple object eliminated the need for quotes, so I can use obj[var1 + var2]
, wich worked :)
事实上,不需要数组对象,一个简单的对象就可以完成这项工作;此外,数组还需要在方括号内使用引号obj["var1 + var2"]
来访问对象属性的值,而不是与索引关联的值(我认为);引号转换"var1 + var2"
为字符串。使用一个简单的对象消除了对引号的需要,所以我可以使用obj[var1 + var2]
,至极有效:)
Thanks everyone !
谢谢大家 !
回答by Mahib
I did something like like following;
我做了类似以下的事情;
let parentArray = [];
let childArray = [1, 2, 3];
childArray.name = 'my-array-1';
parentArray.push(childArray);
To access that by name in ES6;
在 ES6 中通过名称访问它;
parentArray.filter(x => x.name == 'my-array-1');