javascript二维数组不返回正确的值

时间:2021-04-16 20:18:31

I'm testing out a rock paper scissor lizard spock game. Instead of generating the possible outcomes, I used var matrix = [ ] to capture all the possibilities. at array location matrix[3,0], the string entered was "rock wins". However, I ran console.log(matrix[3,0]) right after the array and it produced a different result.

我正在测试一个石头剪刀蜥蜴史波克游戏。我没有生成可能的结果,而是使用var matrix =[]来捕获所有的可能性。在阵列位置矩阵[3,0],输入的字符串为“rock wins”。然而,我在数组后面运行console.log(矩阵[3,0]),它产生了不同的结果。

can someone tell me why that is?

有人能告诉我为什么吗?

var matrix = [];

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";

log.console(matrix[3,0]); //returns spock wins 

2 个解决方案

#1


4  

No, it does not. It actually returns "rock wins".
In short, you use the incorrect syntax.

不,它不。它实际上返回“石头赢”。简而言之,您使用了不正确的语法。

Why does it happen?

You may miss this part and go right to "How should it be?" if you are not interested in understanding the magic behaviour of JavaScript.

如果您对理解JavaScript的神奇行为不感兴趣,那么您可能会错过这一部分,直接转到“应该怎么做?”

  1. You have declared a 1-dimensional array.
  2. 你已经声明了一个一维数组。
  3. When you try to assign or read the following property matrix[0,0], 0,0 is recognized as an expression which is evaluated to the last number (0 in this case).
  4. 当您尝试分配或读取以下属性矩阵[0,0]时,0,0被识别为一个表达式,它被计算为最后一个数字(在本例中为0)。

It means that

这意味着

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
// ...
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
console.log(matrix[3,0]);

is the same as

是一样的

matrix[0] = "it's a tie";
matrix[1] = "Paper wins";
matrix[2] = "rock wins";
// ...
matrix[3] = "Scissors win";
matrix[4] = "spock wins";
matrix[0] = "rock wins";
console.log(matrix[0]); // returns "rock wins"

Read more about it in this * topic.

在这个*主题中了解更多信息。

The reason of it is that you are using a wrong syntax.

原因是您使用了错误的语法。

How should it be?

That's how you access value in multidimensional array in JavaScript:

这就是如何在JavaScript中访问多维数组中的值:

var val = arr[key1][key2][key3]; // not arr[key1, key2, key3]

That's how your code should look like:

你的代码应该是这样的:

var matrix = [];
for (var i = 0; i < 4; i++) matrix[i] = [];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
// ...
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";
document.write(matrix[3][0]);  // not matrix[3,0]

Another good option is to describe your 2D array this way:

另一个不错的选择是这样描述你的2D数组:

var matrix = [
    ["it's a tie", "Paper wins", "rock wins"],
    ["Paper wins", "it's a tie", "Scissors wins"]
    // ...     
];

#2


1  

In your code:

在你的代码:

matrix[0,0] = "it's a tie";

the part in square brackets is an expression that is evaluated and coerced to string. The expression 0,0 returns the value after the last "," and evaluates to 0, 0,1 evaluates to 1, and so on.

方括号中的部分是被求值并强制为字符串的表达式。表达式0,0返回最后一个“,”后的值,并计算为0,0,1计算为1,依此类推。

So:

所以:

var matrix = [];

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";

Creates the "one dimensional" array of the last value assigned to each index:

创建分配给每个索引的最后一个值的“一维”数组:

["rock wins", "Scissors win", "it's a tie", "Scissors win", "spock wins"]

and

matrix[3,0] 

evaluates to

计算结果为

matrix[0]

hence you get "rock wins".

因此你得到了“石头赢”。

What you want is:

你想要的是:

var matrix = [[],[],[],[]];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
matrix[0][3] = "rock wins";
matrix[0][4] = "Spock wins";
matrix[1][0] = "Paper wins";
matrix[1][1] = "it's a tie";
matrix[1][2] = "Scissors win";
matrix[1][3] = "Lizard wins";
matrix[1][4] = "Paper wins";
matrix[2][0] = "rock wins";
matrix[2][1] = "Scissors win";
matrix[2][2] = "it's a tie";
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";

console.log(matrix[3][0]); // rock wins

You could also use an array literal:

你也可以使用数组文字:

var matrix = [
              ["it's a tie","Paper wins","rock wins","rock wins","Spock wins"],
              ["Paper wins","it's a tie","Scissors win","Lizard wins","Paper wins"],
              ["rock wins","Scissors win","it's a tie","Scissors win","spock wins"],
              ["rock wins"]
             ];

#1


4  

No, it does not. It actually returns "rock wins".
In short, you use the incorrect syntax.

不,它不。它实际上返回“石头赢”。简而言之,您使用了不正确的语法。

Why does it happen?

You may miss this part and go right to "How should it be?" if you are not interested in understanding the magic behaviour of JavaScript.

如果您对理解JavaScript的神奇行为不感兴趣,那么您可能会错过这一部分,直接转到“应该怎么做?”

  1. You have declared a 1-dimensional array.
  2. 你已经声明了一个一维数组。
  3. When you try to assign or read the following property matrix[0,0], 0,0 is recognized as an expression which is evaluated to the last number (0 in this case).
  4. 当您尝试分配或读取以下属性矩阵[0,0]时,0,0被识别为一个表达式,它被计算为最后一个数字(在本例中为0)。

It means that

这意味着

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
// ...
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";
console.log(matrix[3,0]);

is the same as

是一样的

matrix[0] = "it's a tie";
matrix[1] = "Paper wins";
matrix[2] = "rock wins";
// ...
matrix[3] = "Scissors win";
matrix[4] = "spock wins";
matrix[0] = "rock wins";
console.log(matrix[0]); // returns "rock wins"

Read more about it in this * topic.

在这个*主题中了解更多信息。

The reason of it is that you are using a wrong syntax.

原因是您使用了错误的语法。

How should it be?

That's how you access value in multidimensional array in JavaScript:

这就是如何在JavaScript中访问多维数组中的值:

var val = arr[key1][key2][key3]; // not arr[key1, key2, key3]

That's how your code should look like:

你的代码应该是这样的:

var matrix = [];
for (var i = 0; i < 4; i++) matrix[i] = [];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
// ...
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";
document.write(matrix[3][0]);  // not matrix[3,0]

Another good option is to describe your 2D array this way:

另一个不错的选择是这样描述你的2D数组:

var matrix = [
    ["it's a tie", "Paper wins", "rock wins"],
    ["Paper wins", "it's a tie", "Scissors wins"]
    // ...     
];

#2


1  

In your code:

在你的代码:

matrix[0,0] = "it's a tie";

the part in square brackets is an expression that is evaluated and coerced to string. The expression 0,0 returns the value after the last "," and evaluates to 0, 0,1 evaluates to 1, and so on.

方括号中的部分是被求值并强制为字符串的表达式。表达式0,0返回最后一个“,”后的值,并计算为0,0,1计算为1,依此类推。

So:

所以:

var matrix = [];

matrix[0,0] = "it's a tie";
matrix[0,1] = "Paper wins";
matrix[0,2] = "rock wins";
matrix[0,3] = "rock wins";
matrix[0,4] = "Spock wins";
matrix[1,0] = "Paper wins";
matrix[1,1] = "it's a tie";
matrix[1,2] = "Scissors win";
matrix[1,3] = "Lizard wins";
matrix[1,4] = "Paper wins";
matrix[2,0] = "rock wins";
matrix[2,1] = "Scissors win";
matrix[2,2] = "it's a tie";
matrix[2,3] = "Scissors win";
matrix[2,4] = "spock wins";
matrix[3,0] = "rock wins";

Creates the "one dimensional" array of the last value assigned to each index:

创建分配给每个索引的最后一个值的“一维”数组:

["rock wins", "Scissors win", "it's a tie", "Scissors win", "spock wins"]

and

matrix[3,0] 

evaluates to

计算结果为

matrix[0]

hence you get "rock wins".

因此你得到了“石头赢”。

What you want is:

你想要的是:

var matrix = [[],[],[],[]];

matrix[0][0] = "it's a tie";
matrix[0][1] = "Paper wins";
matrix[0][2] = "rock wins";
matrix[0][3] = "rock wins";
matrix[0][4] = "Spock wins";
matrix[1][0] = "Paper wins";
matrix[1][1] = "it's a tie";
matrix[1][2] = "Scissors win";
matrix[1][3] = "Lizard wins";
matrix[1][4] = "Paper wins";
matrix[2][0] = "rock wins";
matrix[2][1] = "Scissors win";
matrix[2][2] = "it's a tie";
matrix[2][3] = "Scissors win";
matrix[2][4] = "spock wins";
matrix[3][0] = "rock wins";

console.log(matrix[3][0]); // rock wins

You could also use an array literal:

你也可以使用数组文字:

var matrix = [
              ["it's a tie","Paper wins","rock wins","rock wins","Spock wins"],
              ["Paper wins","it's a tie","Scissors win","Lizard wins","Paper wins"],
              ["rock wins","Scissors win","it's a tie","Scissors win","spock wins"],
              ["rock wins"]
             ];