在 javascript 中创建和访问二维数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12652849/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:57:03  来源:igfitidea点击:

Creating and Accessing 2-dimensional arrays in javascript

javascriptmultidimensional-array

提问by Komal Waseem

I'm confused about how to create and access 2-dimensional arrays in javascript. Below is an array declaration in which I'm storing names of people and then the src for their image. When I try to access myArray[0][0] element I get 'D' and when I try to access myArray[0,0], I get Donald Duck. How can I access the img src myArray[0][0] = "assets/scrybe.jpg" ?

我对如何在 javascript 中创建和访问二维数组感到困惑。下面是一个数组声明,我在其中存储人名,然后是他们图像的 src。当我尝试访问 myArray[0][0] 元素时,我得到“D”,当我尝试访问 myArray[0,0] 时,我得到唐老鸭。如何访问 img src myArray[0][0] = "assets/scrybe.jpg" ?

JS code:

JS代码:

var myArray = new Array(1);

myArray[0] = "Donald Duck";
myArray[1] = "Winnie Pooh";
myArray[2] = "Komal Waseem";
myArray[3] = "Hockey";
myArray[4] = "Basketball";
myArray[5] = "Shooting";
myArray[6] = "Mickey Mouse";

myArray[0][0] = "assets/scrybe.jpg";
myArray[1][0] = "assets/scrybe.jpg";
myArray[2][0] = "assets/scrybe.jpg";
myArray[3][0] = "assets/scrybe.jpg";
myArray[4][0] = "assets/scrybe.jpg";
myArray[5][0] = "assets/scrybe.jpg";
myArray[6][0] = "assets/scrybe.jpg";

回答by 0x499602D2

Firstly, to create an array, it's better to use the square bracket notation ( []):

首先,要创建数组,最好使用方括号表示法 ( []):

var myArray = [];

This is the way you emulate a multi-demensional array in JavaScript. It's one or more arrays inside an array.

这是您在 JavaScript 中模拟多维数组的方式。它是一个数组中的一个或多个数组。

var myArray = [
    [], [] // two arrays
];

If you're asking if there's a way to declare an array as multi-dimensional, then no, there isn't. You can access them like this:

如果您问是否有办法将数组声明为多维数组,那么不,没有。您可以像这样访问它们:

myArray[0][0]; // the 1st element of the first array in myArray
myArray[1][1]; // the 2nd element of the second array in myArray

Here is the code you were probably looking for:

这是您可能正在寻找的代码:

var myArray = [
    ["Donald Duck", "assets/scrybe.jpg"],
    ["Winnie Pooh", "assets/scrybe.jpg"],
    ["Komal Waseem", "assets/scrybe.jpg"]
    [/* and so on...*/]
];

But since you're giving all the names the same URL, then you can use a forloop instead to do this faster:

但是由于您为所有名称提供了相同的 URL,因此您可以使用for循环来更快地执行此操作:

for (var i = 0; i < myArray.length; i++) {
    myArray[i][1]  = "assets/scrybe.jpg";
}

回答by Pointy

Perhaps what you really want is an array of objects:

也许你真正想要的是一个对象数组:

var myArrray = [
  { name: "Donald Duck", image: "assets/scrybe.jpg" },
  { name: "Winnie Pooh", image: "assets/scrybe.jpg" },
  // ...
];

JavaScript doesn't really have 2-dimensional arrays, as such. What you can do is create an array such that each element is also an array.

JavaScript 并没有真正的二维数组。您可以做的是创建一个数组,使每个元素也是一个数组。

var twoDim = [ [], [], [], [], [] ];

In your case, however, I don't think that structure will help much.

但是,在您的情况下,我认为这种结构不会有太大帮助。

回答by sajawikio

No it cannot be done like that. Must either do:

不,不能那样做。必须这样做:

var myArray = new Array(1);

myArray[0] = new Array(1);

myArray[0][0] = "Donald Duck";
myArray[0][1] = "assets/scrybe.jpg";

Or use JS syntax

或者使用JS语法

var myJSObj = {"Donald" :"assets/scrybe.jpg", "Donald2": "assets/scrybe2.jpg" };

回答by Quentin Del

I had the same issues and I used minBy from Lodash

我遇到了同样的问题,我使用了 Lodash 的 minBy

https://lodash.com/docs/4.17.4#minBy

https://lodash.com/docs/4.17.4#minBy

var objects = [{ 'n': 1 }, { 'n': 2 }];

_.minBy(objects, function(o) { return o.n; });
// => { 'n': 1 }

// The `_.property` iteratee shorthand.
_.minBy(objects, 'n');
// => { 'n': 1 }