简体   繁体   English

为什么此javascript代码不起作用?

[英]Why isn't this javascript code working?

var arrayi = new Array();
for (var i=0; i<=9; i++)
{
    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + "" + o;
    }
}
for (var i = 0, j = 9; i <= 9; i++, j--)  
  document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);

I'm trying to assign 00 to arrayi[0][0], 62 to arrayi[6][2] etc.. and then display [0][9], [1][8]... 我正在尝试将00分配给arrayi [0] [0],将62分配给arrayi [6] [2]等。然后显示[0] [9],[1] [8] ...

Arrays in JavaScript are one dimensional. JavaScript中的数组是一维的。

So arrayi[0] is undefined , then arrayi[0][0] becomes undefined[0] which obviously doesn't work. 所以arrayi[0]undefined ,然后arrayi[0][0]变成undefined[0] ,这显然是行不通的。

You need to create a two dimensional array by assigning arrays to the indexes of arrayi , there's also an error with the actual value that's being assigned: 您需要通过将数组分配给arrayi的索引来创建二维数组,实际分配的值也存在错误:

var arrayi = []; // [] is the favored shorthand of new Array()
for (var i=0; i<=9; i++) { // always place { on the same line, 
                           // automatic semi colon insertion can screw you up in JavaScript

    arrayi[i] = []; // assign a new empty array
    for (var o=0; o<=9; o++) {
        arrayi[i][o]= i + '' + o; // i and o are both numbers so you will assign their sum
                                  // need to convert them to strings in order to concatenate them
    }
}

Concerning automatic semicolon insertion screwing you up, take a look at this: 关于自动分号插入让您不知所措,请看以下内容:

return  // js will insert a semi colon here, so this will return undefined
{ // no syntax errors, gets parsed as a block, even though there is block scope in JS
    foo: 2 // parsed as a label... single expression evaluation then makes the 2 work
} // another semi colon gets inserted here

So JS is fixing your code... the wrong way :) 所以JS正在修复您的代码...错误的方式:)

Update 更新资料

It's a bit unclear to me what you exactly want to do, if you want to split a number into it's to decimal places and then assign that, than you will first have to make sure that your arrays are big enough and then you have to split the number: 对我来说,您究竟想做什么是不清楚的,如果您想将数字拆分为小数位然后分配,那么首先必须确保数组足够大,然后再拆分号码:

var i = 62;
var s = (i).toString(); // convert the number 62 to a string
s = i < 10 ? '0' + i : s; // make sure that "9" will become "09"
var p = s.split(''); // ["6", "2"];
arrayi[+p[0]][+p[1]] = i; // assign the value, + will convert the strings to a number without the horrible parseInt

Your sixth line has i + o , where i and o are Numbers, which will be added as numbers. 您的第六行是i + o ,其中i和o是数字,这将作为数字添加。 0 + 0 = 0 , and 6 + 2 = 8 . 0 + 0 = 0 ,和6 + 2 = 8 To concatenate strings you need to convert the numbers to strings. 要连接字符串,您需要将数字转换为字符串。 The simplest way to do that is to add an empty string; 最简单的方法是添加一个空字符串。 arrayi[i][o] = i + "" + o

Change this, 改变这个

arrayi[i][o]=i + o;

with; 与;

arrayi[i][o]= i + "" + o;

I'm guessing you're aiming at something like this: 我猜您的目标是这样的:

var arrayi = new Array(10);
for (var i=0; i<=9; i++)
{
    arrayi[i] = new Array(10);

    for (var o=0; o<=9; o++)
    {
        arrayi[i][o]=i + o;
    }
}

for (var i = 0; i <= 9; i++)  
    for (var j = 0; j <= 9; j++)
      document.write("arrayi[" + i + "][" + j + "]= " + arrayi[i][j] + "<br>");

When you run that code, you get the error ReferenceError: arrayi is not defined . 运行该代码时,出现错误ReferenceError: arrayi is not defined

You need to set arrayi[i] before assigning another item to it as if it's an array. 您需要先设置arrayi [i],然后再为其分配另一个项目,就好像它是一个数组一样。 I suggest using push when possible to set array elements. 我建议尽可能使用push设置数组元素。

How about this code: 这段代码怎么样:

var arrayi=[];for (var i=0; i<=9; i++)
  {
  arrayi.push([]);
  for (var o=0; o<=9; o++)
    { 
    arrayi[i].push([i +''+ o]);
    }
  }
  for (var i = 0, j = 9; i <= 9; i++, j--){
    console.log("arrayi[" + i + "][" + j + "]= " + arrayi[i][j]);
    }

Output: 输出:

arrayi[0][9]= 09
arrayi[1][8]= 18
arrayi[2][7]= 27
arrayi[3][6]= 36
arrayi[4][5]= 45
arrayi[5][4]= 54
arrayi[6][3]= 63
arrayi[7][2]= 72
arrayi[8][1]= 81
arrayi[9][0]= 90

That's what you wanted, right? 那就是你想要的,对吗?

One extra tip; 一小费 I suggest var array=[] rather than new Array() in JavaScript. 我建议使用var array=[]而不是JavaScript中的new Array()

Try either 尝试之一

arrayi[i][o]=i * 10 + o;

or 要么

arrayi[i][o]=String(i) + String(o);

要对此进行数字分配,您需要:

arrayi[i][o] = (i*10)+o;

document.write("arrayi[" + i + "][" + j + "]= " + a[i][j]);

您调用a而不是arrayi

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

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