2016-05-13 6 views
0

私は次のテキストファイル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つだけ取得します。

+0

CVSまたはCSV? ;-) – RobG

答えて

1

resを定義して重複した値を削除するときは、チェーンの最後にフィルタを追加するだけです。 その問題は数回解決されました:Unique values in an array

それは次のようになります。

var res = stop_list 
    .map(function(val, key) { 
    return val.split(re)[index]; 
    }) 
    .filter(function(value, index, self) { 
    return self.indexOf(value) === index; 
    }); 
1

私はヒューゴの答えが好きです。あなたはプラットフォームがユニークであることを確認したいので、私はちょうどカップルを追加します。

var stop_list = response.split(/(\r\n|\n)/); 
var re = /,/; 
var headers = stop_list.shift().split(re); 
var index = headers.indexOf("stop_name"); 
var platIndex = headers.indexOf("platform_code"); 

var res = stop_list 
    .map(function(val, key) { 
    return val.split(re)[index] ? val.split(re)[index] + ',' + val.split(re)[platIndex] : ''; 
    }) 
    .filter(function(value, index, self) { 
    return value && self.indexOf(value) === index; 
    }); 

var str = ''; 
     var i; 
     for (i = 0; i < res.length; i++) { 
      str += '<option value="'+res[i].split(re)[0]+'" />'; 
     } 

console.log(str); 
//str: <option value="San Francisco Caltrain" /><option value="22nd St Caltrain" /><option value="22nd St Caltrain" /> 

すべてのplatform_codesはstop_nameごとに一意であるため、上記の出力を得るために最初の2つを両方ともNBに変更しました。

+0

あなたの答えは依然として私に重複した結果をもたらします –

+0

"stop_nameとplate_formが同じ場合は、stop_nameの結果セットを1つだけ取得します。" stop_nameとプラットフォームは異なるため、複数の結果が得られます。あなたは実際にプラットフォームに関係なく1つの結果がほしいと思うように聞こえます。 – curtwphillips

関連する問題