我正在寻找一种方法来读取外部文本文件map.txt
(在我自己的服务器中分配,而不是在客户端的计算机中),并将包含在其中的数据存储在二维数组中{ {1}}使用Javascript。
map[][]
文件将填充数字,每个行的长度都是固定的:
map.txt
因此,我希望它以21123301201200
12030201303321
22321321021312
12312310001230
12312321221220
和map
的方式将其存储在二维数组map[0][0] = 2
中 - 在此示例中。这甚至可能吗?如果是这样,它是如何完成的?
谢谢!
答案 0 :(得分:0)
您可以使用XMLHttpRequest
来获取该文件。看看:http://www.quirksmode.org/js/xmlhttp.html
并使用此函数创建数组:
function handleRequest(req){
var a=req.responseText.split('\n');
var len=a.length;
for(var i=0;i<len;i++){
a[i]=a[i].split('');
}
return a;
}
此功能应返回:
[["2","1","1","2","3","3","0","1","2","0","1","2","0","0"],
["1","2","0","3","0","2","0","1","3","0","3","3","2","1"],
["2","2","3","2","1","3","2","1","0","2","1","3","1","2"],
["1","2","3","1","2","3","1","0","0","0","1","2","3","0"],
["1","2","3","1","2","3","2","1","2","2","1","2","2","0"]]
使用map[4][13]
代替map[13][4]
来获取示例中的最后一个元素