admin管理员组文章数量:1416050
Basically I have my code set up like this:
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) will just give me:
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,.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,.html,0,ctsf,SB,1
And here is how the content in my stop.txt file look like:
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,.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,.html,0,ctsf,SB,1
70021,70021,22nd St Caltrain,37.757599,-122.39188,1,.html,0,ct22,NB,2
70022,70022,22nd St Caltrain,37.757583,-122.392404,1,.html,0,ct22,SB,2
70031,70031,Bayshore Caltrain,37.709537,-122.401586,1,.html,0,ctba,NB,1
70032,70032,Bayshore Caltrain,37.709544,-122.40198,1,.html,0,ctba,SB,1
70041,70041,So. San Francisco Caltrain Station,37.65589,-122.40487,1,.html,0,ctssf,NB,2
70042,70042,So. San Francisco Caltrain Station,37.655946,-122.405018,1,.html,0,ctssf,SB,2
70051,70051,San Bruno Caltrain,37.631128,-122.411968,1,.html,0,ctsb,NB,1
This file is in the cvs style. What I want is to get the stop_name, stop_id, stop_code, stop_long.... of each item in the array.
Using javascript Promise api would be great
Basically I have my code set up like this:
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) will just give me:
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./stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain./stations/sanfranciscostation.html,0,ctsf,SB,1
And here is how the content in my stop.txt file look like:
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./stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain./stations/sanfranciscostation.html,0,ctsf,SB,1
70021,70021,22nd St Caltrain,37.757599,-122.39188,1,http://www.caltrain./stations/22ndstreetstation.html,0,ct22,NB,2
70022,70022,22nd St Caltrain,37.757583,-122.392404,1,http://www.caltrain./stations/22ndstreetstation.html,0,ct22,SB,2
70031,70031,Bayshore Caltrain,37.709537,-122.401586,1,http://www.caltrain./stations/bayshorestation.html,0,ctba,NB,1
70032,70032,Bayshore Caltrain,37.709544,-122.40198,1,http://www.caltrain./stations/bayshorestation.html,0,ctba,SB,1
70041,70041,So. San Francisco Caltrain Station,37.65589,-122.40487,1,http://www.caltrain./stations/southsanfranciscostation.html,0,ctssf,NB,2
70042,70042,So. San Francisco Caltrain Station,37.655946,-122.405018,1,http://www.caltrain./stations/southsanfranciscostation.html,0,ctssf,SB,2
70051,70051,San Bruno Caltrain,37.631128,-122.411968,1,http://www.caltrain./stations/sanbrunostation.html,0,ctsb,NB,1
This file is in the cvs style. What I want is to get the stop_name, stop_id, stop_code, stop_long.... of each item in the array.
Using javascript Promise api would be great
Share Improve this question edited Apr 23, 2016 at 6:06 LearnToday asked Apr 23, 2016 at 3:42 LearnTodayLearnToday 2,9029 gold badges41 silver badges73 bronze badges 1-
"Using javascript Promise api would be great" Use of
Promise
is not necessary to return expected result described at original Question. See stackoverflow./questions/14220321/… – guest271314 Commented Apr 23, 2016 at 5:52
1 Answer
Reset to default 3You can use RegExp
/\n+/
at converttxttoArray
; Array.prototype.shift()
to remove, store first item of stop_list
array ;.split()
with RegExp
/,/
to convert string to an array; retrieve index of "stop_name"
within removed first item within stop_list
using .indexOf()
; Array.prototype.map()
,.split()
with /,/
as parameter to return item at index of "stop_name"
within remainder of 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");
}
plnkr http://plnkr.co/edit/dhr6hQAb151c8oFBTvqk?p=preview
本文标签: textHow to loop through a txt file and get the values in javascriptStack Overflow
版权声明:本文标题:text - How to loop through a .txt file and get the values in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745247043a2649601.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论