简体   繁体   English

在javascript中创建一个多维数组

[英]Create a multidimensional array in javascript

i'm not really sure, how to explain this, but i trying to create a multidimensional anonymes array in javascript. 我不太确定,如何解释这个,但我试图在javascript中创建一个多维anonymes数组。 I'm new to Javascript and mostly did jQuery stuff so far. 我是Javascript的新手,到目前为止大多数都是jQuery的东西。 But now i need this and what i am trying to achieve should work like this somehow: 但现在我需要这个,我想要实现的应该以某种方式工作:

var
    outerArray = [],
    innerArray = new Array("position", "value", "done");

outerArray.push(innerArray);

// Then i want to use
outerArray["dynamicNameHere"]["position"] = 30;
outerArray["dynamicNameHere"]["value"] = 50;
outerArray["dynamicNameHere"]["done"] = false;

outerArray["otherDynamicNameHere"]["position"] = 100;
outerArray["otherDynamicNameHere"]["value"] = 500;
outerArray["otherDynamicNameHere"]["done"] = true;

The array is two-dimensional. 该阵列是二维的。 Some values are booleans, but most of them are just integers as you can see in my example code. 有些值是布尔值,但大多数值只是整数,正如您在示例代码中看到的那样。 I hope my code help you understand my intention, because i'm not sure, how to explain it further. 我希望我的代码可以帮助你理解我的意图,因为我不确定,如何进一步解释它。 And, i know this code above doesn't work, but i have no idea, how to create something like this or if this is even possible with javascript (in PHP it is at least). 而且,我知道上面的代码不起作用,但我不知道,如何创建这样的东西,或者甚至可以使用javascript(在PHP中至少是这样)。 This was just some code snippets i found about arrays and hoped this could work. 这只是我发现的关于数组的一些代码片段,并希望这可以工作。 But it didn't. 但事实并非如此。 But to me "push" seems like the way to do this? 但对我来说“推”似乎是这样做的方式?

Thanks for your help and time. 谢谢你的帮助和时间。

It seems more, that you need an object and not an array: 看起来更多,你需要一个对象而不是一个数组:

var outerArray = {};

// add elements
outerArray[ 'dynamicNameHere' ] = {
  'position': 30,
  'value': 50,
  'done': false
};

// access those
console.log( outerArray[ 'dynamicNameHere' ][ 'position' ] );
// or
console.log( outerArray[ 'dynamicNameHere' ].position );

Arrays in JavaScript are for numerical indices only. JavaScript中的数组仅用于数字索引。 There are no associative arrays . 没有关联数组

You seem to want plain, nested objects: 您似乎想要简单的嵌套对象:

outerObject = new Object();

outerObject["dynamicNameHere"] = new Object();
outerObject["dynamicNameHere"]["position"] = 30;
outerObject["dynamicNameHere"]["value"] = 50;
outerObject["dynamicNameHere"]["done"] = false;

outerObject["otherDynamicNameHere"] = new Object();
outerObject["otherDynamicNameHere"]["position"] = 100;
outerObject["otherDynamicNameHere"]["value"] = 500;
outerObject["otherDynamicNameHere"]["done"] = true;

But lots easier to use is the object literal notation: 但是更容易使用的是对象文字符号:

var outerObject = {
    dynamicNameHere: {
        position: 30,
        value: 50,
        done: false
    },
    otherDynamicNameHere: {
        position: 100,
        value: 500,
        done: true
    }
}

Though, if those property names are really dynamic you will need to use the bracket notation for them: 但是,如果这些属性名称非常动态,则需要使用括号表示法:

var outerObject = {}, // empty object
    dynamicName = …,
    otherDynamicName = …;
outerObject[dynamicName] = {
    position: 30,
    value: 50,
    done: false
};
outerObject[otherDynamicName] = {
    position: 100,
    value: 500,
    done: true
};

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

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