教你在javascript中定义二维数组!

你应该知道js中定义一维数组的方法,我经常是这样定义的

arr=new Array();

但是定义二维数组又如何呢?JS中貌似不能直接定义二维数组,我们定义二维数组可以这么做:

//定义3行4列型二维数组
row=3;
col=4;
arr=new Array();
for(i=0;i<row;i++){
  arr[i]=new Array();//必需,否则下面arr[i][j]报错
  for(j=0;j<col;j++)
     arr[i][j]='('+(i+1)+','+(j+1)+')';
}

这样对应生成的二维数组为:

(1,1) (1,2) (1,3) (1,4)
(2,1) (2,2) (2,3) (2,4)
(3,1) (3,2) (3,3) (3,4)

虽然有点不方便,但是还是蛮好用的。
原创文章,转载请注明出处。

WebRebuild.ORG 北京第二届年会 (2010年7月17日·北京)互助推广

9 Responses to 教你在javascript中定义二维数组! »

  1. Dimitri 评论 2009-02-05 23:27

    你把博客搞得跟cms快一样了。。。

    回复

  2. Snow 评论 2009-02-06 09:58

    :( 矩阵

    回复

  3. Hicro 评论 2009-02-06 21:44

    对于javascript来说对象才是王道,就像python的字典一样。

    回复

  4. 醉倚西风 评论 2009-02-07 12:43

    :P :kiss: :D :( :o hh: ;) :mad: :) :cool: :o :lol: 表情真好玩

    回复

    joe 回复 二月 7th, 2009 at 13:05

    :mad: 你一下子发这么多表情。。。

    回复

  5. LD 评论 2009-02-07 13:38

    good, 学习

    回复

    test 回复 二月 8th, 2009 at 11:13

    测试~

    回复

  6. aunsen 评论 2009-02-09 23:05

    首页又小改了下啊 :(

    回复

    joe 回复 二月 10th, 2009 at 09:15

    :o hh: 没怎么改哈

    回复

Leave a Reply

Email address is not published

You should say a Chinese word to pass spam check. If you can not input Chinese, just copy 你好 and paste them into comment text box.