0

我相信我要做的是在一个数组中声明几个数组。在文本文档中,我有以下内容:

"一","いち","one"
"二","に","two"
"三","さん","three"
"四","し・よん","four"
"五","ご","five"

我想将其自动放入一个数组中,并将项目分配为 3 个组,因此例如 set_one[0][1] 将是“いち”,set_one[3][2] 将是“四”,依此类推。

对于我的一生,我什至无法弄清楚如何从纯文本文档中逐行读取值,更不用说尝试将它们自动分配到数组中了..所以我手动尝试了。我有:

var set_one = new Array(new Array("一", "いち","one"), new Array("二", "に","two", new Array("三", "さん","three", new Array("四", "よん・し","four", new Array("五", "ご","five");

其中,当 document.write(set_one[3][2]); 被调用,什么都没有发生。我什至尝试了一个 for 循环来查看 (set_one) 数组中是否存在任何东西,尽管据我所知,什么都没有。

至少可以说,在这台 Windows 机器上工作很困难,因为我没有可用的调试工具,而且它没有有效的 Internet 连接!我究竟做错了什么?有没有更好的方法来做到这一点?是否甚至可以将值自动逐行读取到数组中,然后根据逗号值将值分配给各个数组?

4

2 回答 2

3

您没有正确创建数组。例如,当您有:

new Array("二", "に","two", new Array("三", "さん","three"))

您实际上是在创建一个单元素数组,其中第三个位置本身就是另一个数组。要么使用:

new Array (new Array("二", "に","two"), new Array("三", "さん","three"))

或者在 JavaScript 中创建数组的更简单、更容易混淆的方法:

var set_one = [ 
  [ "一","いち","one" ],
  [ "二","に","two" ],
  [ "三","さん","three" ],
  [ "四","し・よん","four" ],
  [ "五","ご","five" ] 
];
set_one[0][1]; // いち
于 2012-09-06T13:03:30.657 回答
0
var set_one = [
["一","いち","one"], 
["二","に","two"], 
["三","さん","three"], 
["四","し・よん","four"], 
["五","ご","five"]
];
于 2012-09-06T13:03:17.707 回答