将服务器端的.txt文件加载到JavaScript数组中

问题描述:

我想加载一个。 TXT文件看起来像:将服务器端的.txt文件加载到JavaScript数组中

0,0:0 
0,1:1 
0,2:2 
0,3:3 
1,0:0 
1,1:1 
1,2:2 
1,3:3 

成多维JavaScript数组称为MAP_ID [X] [Y]。

map_id[0][0] = 0;因为在.txt文件0,0 = 0

可能吗?

+2

您正在查找的关键字是“Ajax”。 – Quentin 2014-09-06 17:00:19

您可以使用AJAX。因为JavaScript是太难用了AJAX(Internet Explorer的是有罪的),你可以使用JQuery的

$.ajax({ 
    type: "GET", 
    url: "file.txt",     // or path to file 
    success: function(content) { 
     // parse the content (content is the file content) 
    }, 
    error: function() { 
     console.log('error'); 
    } 
}); 

其中解析的内容是这样的:

var lines = content.split('\n'); 
for (var i = 0 ; i < lines.length ; i++) { 
    // take index1, index2, value and store them in a 2d array (parse lines[i]) 
} 
+0

我没有站在索引1和索引2来自 – TheProWolfPcGames 2014-09-06 17:18:22

+0

一条线看起来像这样:'0,1:1'。这是'线[我]'。 只需将0作为“index1”,将1作为“index2”,将1作为值。提示:“分裂”。 – 2014-09-06 17:21:15

+0

谢谢我现在得到它 – TheProWolfPcGames 2014-09-06 17:25:37