私は次のテキストファイルstops.txt
を持っています。重複するJavaScript配列の結果セットから1行だけを取得する
get('../data/stops.txt').then(function(response) {
//console.log("Success!", response.split(/(\r\n|\n)/));
var stop_list = response.split(/(\r\n|\n)/);
var re = /,/;
var headers = stop_list.shift().split(re);
var index = headers.indexOf("stop_name");
//Removing [index] at return val.split(re)[index]; should return an array of arrays of each split value
var res = stop_list.map(function(val, key) {
return val.split(re)[index];
}).filter(Boolean);
var str = '';
var i;
for (i = 0; i < res.length; i++) {
str += '<option value="'+res[i]+'" />';
}
var s_list=document.getElementById("slist");
s_list.innerHTML = str;
}, function(error) {
console.error("Failed!", error);
});
これは完璧に動作します:
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
はここdatalist
にstop_namesインデックスを取得するための私のjavascript関数です。しかし、最初の行が2行目と同じ停止名を持っているので。
stop_nameとplate_formが同じ場合、stop_nameの結果セットを1つだけ取得します。
CVSまたはCSV? ;-) – RobG