[英]Define associative array of arrays
I want to define an associative array like this 我想像这样定义一个关联数组
var theVar = [
{ "100", [0, 1, 2] },
{ "101", [3, 4, 5] }
]
Essentially I want to be able to access an array of three numbers by specifying the custom index. 基本上我希望能够通过指定自定义索引来访问三个数字的数组。
However, no matter what I try I cannot make it work. 但是,无论我尝试什么,我都无法使其发挥作用。
I know I can define it as: 我知道我可以将它定义为:
theVar["100"] = [0, 1, 2];
theVar["101"] = [1, 2, 3];
But I am setting this somewhere else and I'd prefer to be able to set it in a single statement. 但我将其设置在其他地方,我宁愿能够在一个声明中设置它。
theVar = {
"100": [0, 1, 2],
"101": [3, 4, 5]
}
might do the trick. 可能会做的伎俩。 You can then access using theVar["101"]
(or theVar[101]
for that matter). 然后,您可以使用theVar["101"]
(或者theVar[101]
)进行访问。
(As var
is also a keyword in JavaScript, using it as a variable name is very likely to cause problems.) (由于var
也是JavaScript中的关键字 ,因此将其用作变量名很可能会导致问题。)
Have a look at the JSON syntax, I think It could inspire the building of your data structures in a way that will be flexible, correct and complex as you want. 看看JSON语法,我认为它可以激发您的数据结构的构建,使其灵活,正确和复杂。
This page has a lot of information and example that are helpful for you. 此页面包含大量有助于您的信息和示例。
For instance look at this: 比如看看这个:
var employees = { "accounting" : [ // accounting is an array in employees.
{ "firstName" : "John", // First element
"lastName" : "Doe",
"age" : 23 },
{ "firstName" : "Mary", // Second Element
"lastName" : "Smith",
"age" : 32 }
], // End "accounting" array.
"sales" : [ // Sales is another array in employees.
{ "firstName" : "Sally", // First Element
"lastName" : "Green",
"age" : 27 },
{ "firstName" : "Jim", // Second Element
"lastName" : "Galley",
"age" : 41 }
] // End "sales" Array.
} // End Employees
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.