我相信我要做的是在数组中声明几个数组。 在文本文档中,我有以下内容:
"一","いち","one"
"二","に","two"
"三","さん","three"
"四","し・よん","four"
"五","ご","five"
我想自动放入一个数组,项目分配为3组,所以例如set_one [0] [1]将是“いち”,set_one [3] [2]将是“4”,等等。
对于我的生活,我无法弄清楚如何从纯文本文档中逐行读取值,更不用说尝试自动将它们分配到数组中了。所以我尝试了手动。我有:
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连接!我究竟做错了什么?有没有更好的方法呢?是否可以逐行自动将值读入数组,然后根据逗号值将值分配给各个数组?
答案 0 :(得分:3)
您没有正确创建阵列。例如,当你有:
new Array("二", "に","two", new Array("三", "さん","three"))
您实际上正在创建一个单元素数组,其中 3rd 位置本身另一个数组。使用:
new Array (new Array("二", "に","two"), new Array("三", "さん","three"))
或者在JavaScript中创建数组的更简单,更简单的方法:
var set_one = [
[ "一","いち","one" ],
[ "二","に","two" ],
[ "三","さん","three" ],
[ "四","し・よん","four" ],
[ "五","ご","five" ]
];
set_one[0][1]; // いち
答案 1 :(得分:0)
var set_one = [
["一","いち","one"],
["二","に","two"],
["三","さん","three"],
["四","し・よん","four"],
["五","ご","five"]
];