在javascript中创建一个多维数组

时间:2022-03-29 01:47:45

i'm not really sure, how to explain this, but i trying to create a multidimensional anonymes array in javascript. I'm new to Javascript and mostly did jQuery stuff so far. But now i need this and what i am trying to achieve should work like this somehow:

我不太确定,如何解释这个,但我试图在javascript中创建一个多维anonymes数组。我是Javascript的新手,到目前为止大部分都是jQuery的东西。但是现在我需要这个,而我想要实现的目标应该是这样的:

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). 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?

该阵列是二维的。有些值是布尔值,但大多数值只是整数,正如您在示例代码中看到的那样。我希望我的代码可以帮助你理解我的意图,因为我不确定,如何进一步解释它。并且,我知道上面的代码不起作用,但我不知道,如何创建这样的东西,或者如果这甚至可以使用javascript(在PHP中至少是这样)。这只是我发现的关于数组的一些代码片段,并希望这可以工作。但事实并非如此。但对我来说“推”似乎是这样做的方式?

Thanks for your help and time.

谢谢你的帮助和时间。

2 个解决方案

#1


1  

Arrays in JavaScript are for numerical indices only. There are no associative arrays.

JavaScript中的数组仅用于数字索引。没有关联数组。

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
};

#2


1  

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 );

#1


1  

Arrays in JavaScript are for numerical indices only. There are no associative arrays.

JavaScript中的数组仅用于数字索引。没有关联数组。

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
};

#2


1  

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 );