0

I have the following text file stops.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

Here is my javascript function to get the stop_names index to a datalist:

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);
    });

This works just perfect. But since the first row is having the same stop name as row 2.

What I want is to get just one result set of stop_name if stop_name and plate_form is the same.

StepUp
  • 36,391
  • 15
  • 88
  • 148
LearnToday
  • 2,762
  • 9
  • 38
  • 67

2 Answers2

1

All you need to do is add a filter to the end of the chain when defining res to remove duplicate values. That problem has been resolved a few times: Unique values in an array

It would look like this:

var res = stop_list
  .map(function(val, key) {
    return val.split(re)[index];
  })
  .filter(function(value, index, self) { 
    return self.indexOf(value) === index;
  });
Community
  • 1
  • 1
Hugo Silva
  • 6,748
  • 3
  • 25
  • 42
1

I like Hugo's answer. I would add just a couple pieces since you want to verify the platform is unique too.

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" />

All the platform_codes were unique for each stop_name so I changed the first two to both use NB to get the above output.

curtwphillips
  • 5,441
  • 1
  • 20
  • 19
  • Your answer still gives me duplicate results – LearnToday May 15 '16 at 14:38
  • You specified, "What I want is to get just one result set of stop_name if stop_name and plate_form is the same." Since the stop_name and platform are different, it would get more than one result. It sounds like you actually want one result regardless of platform. – curtwphillips May 16 '16 at 21:57