简体   繁体   English

Javascript-多维关联数组

[英]Javascript - multidimensional associative array

I would like to know (if it's possible) how I can push data into a multidimensional associative array with JavaScript like this: 我想知道(如果可能)如何使用JavaScript将数据推入多维关联数组中,如下所示:

myarray['type1']['label1'].push('Data to insert');
myarray['type1']['label1'].push('Data2 to insert');
myarray['type1']['label1'].push('Data3 to insert');


myarray['type2']['label1'].push('Data to insert');
myarray['type2']['label2'].push('Data1 to insert');
myarray['type2']['label2'].push('Data2 to insert');

Here is the answer thanks to Brennan: 感谢布伦南,这是答案:

myarray = {};
myarray['type1'] = {};
myarray['type1']['label1'] = [];
myarray['type1']['label1'].push('Data to insert');
myarray['type1']['label1'].push('Data2 to insert');
myarray['type1']['label1'].push('Data3 to insert');

console.log(myarray);

http://jsfiddle.net/177zu9n4/ http://jsfiddle.net/177zu9n4/

Thanks a lot. 非常感谢。

First, there needs to be an array there to push the data into: 首先,需要有一个数组将数据推送到其中:

myarray = {};
myarray['type1'] = {};
myarray['type1']['label1'] = [];
myarray['type1']['label1'].push('Data to insert');
myarray['type1']['label1'].push('Data2 to insert');
myarray['type1']['label1'].push('Data3 to insert');
myarray= {};
myarray.type1= {};
myarray.type1.label1= "Data to insert";

console.log(myarray.type1.label1);

Is this what you need? 这是您需要的吗? If label1 is somehow meant to hold stuff too, as an array: 如果label1以某种方式也要容纳东西,则作为数组:

myarray= {};
myarray.type1= {};
myarray.type1.label1= [];

myarray.type1.label1.push("data");

console.log(myarray.type1.label1);

Note that I used both object and arrays in the last example, and only objects in the first. 请注意,在上一个示例中我同时使用了对象和数组,在第一个示例中仅使用了对象。 Each have case-specific uses they are better at. 每个案例都有特定的用途,它们比较擅长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM