如何遍历 .txt 文件并获取 javascript 中的值
How to loop through a .txt file and get the values in javascript
基本上我的代码是这样设置的:
function converttxttoArray( filename )
{
var reader = (window.XMLHttpRequest != null )
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
reader.open("GET", filename, false );
reader.send( );
return reader.responseText.split(/(\r\n|\n)/g);
}
var stop_list = converttxttoArray("../data/stops.txt");
var text = "";
var i;
for (i = 0; i < stop_list.length; i++) {
text += stop_list[i] + "<br>";
}
console.log(text) 只会给我:
stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding
70011,70011,San Francisco Caltrain,37.77639,-122.394992,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,SB,1
我的 stop.txt 文件中的内容如下所示:
stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding
70011,70011,San Francisco Caltrain,37.77639,-122.394992,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,SB,1
70021,70021,22nd St Caltrain,37.757599,-122.39188,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,NB,2
70022,70022,22nd St Caltrain,37.757583,-122.392404,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,SB,2
70031,70031,Bayshore Caltrain,37.709537,-122.401586,1,http://www.caltrain.com/stations/bayshorestation.html,0,ctba,NB,1
70032,70032,Bayshore Caltrain,37.709544,-122.40198,1,http://www.caltrain.com/stations/bayshorestation.html,0,ctba,SB,1
70041,70041,So. San Francisco Caltrain Station,37.65589,-122.40487,1,http://www.caltrain.com/stations/southsanfranciscostation.html,0,ctssf,NB,2
70042,70042,So. San Francisco Caltrain Station,37.655946,-122.405018,1,http://www.caltrain.com/stations/southsanfranciscostation.html,0,ctssf,SB,2
70051,70051,San Bruno Caltrain,37.631128,-122.411968,1,http://www.caltrain.com/stations/sanbrunostation.html,0,ctsb,NB,1
这个文件是cvs风格的。
我想要的是获取数组中每个项目的 stop_name、stop_id、stop_code、stop_long。
使用 javascript Promise api 会很棒
您可以在 converttxttoArray
使用 RegExp
/\n+/
; Array.prototype.shift()
删除,存储 stop_list
数组的第一项;.split()
和 RegExp
/,/
将字符串转换为数组;使用 .indexOf()
在 stop_list
中删除的第一项中检索 "stop_name"
的索引; Array.prototype.map()
,.split()
以 /,/
作为 return 项目的参数 "stop_name"
的索引处 stop_list
的剩余部分
window.onload = function() {
function converttxttoArray(filename) {
var reader = (window.XMLHttpRequest != null)
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
reader.open("GET", filename, false);
reader.onload = function() {
var stop_list = this.responseText.split(/\n+/);
var re = /,/;
var headers = stop_list.shift().split(re);
var index = headers.indexOf("stop_name");
var res = stop_list.map(function(val, key) {
return val.split(re)[index];
});
console.log(res);
var text = "";
var i;
for (i = 0; i < stop_list.length; i++) {
text += res[i] + "<br>";
}
console.log(text);
document.body.innerHTML = text;
}
reader.send();
}
converttxttoArray("stops.txt");
}
基本上我的代码是这样设置的:
function converttxttoArray( filename )
{
var reader = (window.XMLHttpRequest != null )
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
reader.open("GET", filename, false );
reader.send( );
return reader.responseText.split(/(\r\n|\n)/g);
}
var stop_list = converttxttoArray("../data/stops.txt");
var text = "";
var i;
for (i = 0; i < stop_list.length; i++) {
text += stop_list[i] + "<br>";
}
console.log(text) 只会给我:
stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding
70011,70011,San Francisco Caltrain,37.77639,-122.394992,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,SB,1
我的 stop.txt 文件中的内容如下所示:
stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding
70011,70011,San Francisco Caltrain,37.77639,-122.394992,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,SB,1
70021,70021,22nd St Caltrain,37.757599,-122.39188,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,NB,2
70022,70022,22nd St Caltrain,37.757583,-122.392404,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,SB,2
70031,70031,Bayshore Caltrain,37.709537,-122.401586,1,http://www.caltrain.com/stations/bayshorestation.html,0,ctba,NB,1
70032,70032,Bayshore Caltrain,37.709544,-122.40198,1,http://www.caltrain.com/stations/bayshorestation.html,0,ctba,SB,1
70041,70041,So. San Francisco Caltrain Station,37.65589,-122.40487,1,http://www.caltrain.com/stations/southsanfranciscostation.html,0,ctssf,NB,2
70042,70042,So. San Francisco Caltrain Station,37.655946,-122.405018,1,http://www.caltrain.com/stations/southsanfranciscostation.html,0,ctssf,SB,2
70051,70051,San Bruno Caltrain,37.631128,-122.411968,1,http://www.caltrain.com/stations/sanbrunostation.html,0,ctsb,NB,1
这个文件是cvs风格的。 我想要的是获取数组中每个项目的 stop_name、stop_id、stop_code、stop_long。
使用 javascript Promise api 会很棒
您可以在 converttxttoArray
使用 RegExp
/\n+/
; Array.prototype.shift()
删除,存储 stop_list
数组的第一项;.split()
和 RegExp
/,/
将字符串转换为数组;使用 .indexOf()
在 stop_list
中删除的第一项中检索 "stop_name"
的索引; Array.prototype.map()
,.split()
以 /,/
作为 return 项目的参数 "stop_name"
的索引处 stop_list
window.onload = function() {
function converttxttoArray(filename) {
var reader = (window.XMLHttpRequest != null)
? new XMLHttpRequest()
: new ActiveXObject("Microsoft.XMLHTTP");
reader.open("GET", filename, false);
reader.onload = function() {
var stop_list = this.responseText.split(/\n+/);
var re = /,/;
var headers = stop_list.shift().split(re);
var index = headers.indexOf("stop_name");
var res = stop_list.map(function(val, key) {
return val.split(re)[index];
});
console.log(res);
var text = "";
var i;
for (i = 0; i < stop_list.length; i++) {
text += res[i] + "<br>";
}
console.log(text);
document.body.innerHTML = text;
}
reader.send();
}
converttxttoArray("stops.txt");
}