如何获取.txt(CSV)文件数组

时间:2016-04-24 10:13:02

标签: javascript arrays arraylist multidimensional-array

我正在尝试获取数组的值(示例trips_list.trip_id应该倒出数组trips_list的所有trip_id。)

.trips.txt file :
route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,shape_id,wheelchair_accessible,bikes_allowed
TaSj-16APR,CT-16APR-Caltrain-Saturday-02,23a,DIRIDON STATION,23,0,cal_tam_sj,,
TaSj-16APR,CT-16APR-Caltrain-Saturday-02,25a,DIRIDON STATION,25,0,cal_tam_sj,,
TaSj-16APR,CT-16APR-Caltrain-Saturday-02,27a,DIRIDON STATION,27,0,cal_tam_sj,,.................

以下是我现在所拥有的:

function get(url) {
  return new Promise(function(resolve, reject) {
    var req = new XMLHttpRequest();
    req.open('GET', url);
    req.onload = function() {
      if (req.status == 200) {
        resolve(req.response);
      }
      else {
        reject(Error(req.statusText));
      }
    };
    req.onerror = function() {
      reject(Error("Network Error"));
    };
    req.send();
  });
}
    get('../data/trips.txt').then(function(response) {
  //console.log("Success!", response.split(/(\r\n|\n)/));
  var trips_list = response.split(/(\r\n|\n)/);
  if(Array.isArray(trips_list)) {
    console.log('true');
  } else {
    console.log('Not An Array')
  }
  var re = /,/;
  var headers = trips_list.shift().split(re);
  var route_id = headers.indexOf("route_id");
  var service_id = headers.indexOf("service_id");
  var trip_id = headers.indexOf("trip_id");
  var trip_headsign = headers.indexOf("trip_headsign");
  var mainArray = trips_list.map(function(val, key) {
    return val.split(re);
  }).filter(Boolean);

  var trip_id = headers.indexOf("trip_id");
  var tripid = trips_list.map(function(val, key) {
    return val.split(re)[trip_id];
  }).filter(Boolean);

   var service_id = headers.indexOf("service_id");
  var serviceid = trips_list.map(function(val, key) {
    return val.split(re)[service_id];
  }).filter(Boolean);

  var route_id = headers.indexOf("route_id");
  var routeid = trips_list.map(function(val, key) {
    return val.split(re)[route_id];
  }).filter(Boolean);


   var tripstr = '';
    var i;
    for (i = 0; i < routeid.length; i++) {
       tripstr += '<div class="tripper"><b>RID:</b> <em>'+routeid[i]+'</em> <b>SID:</b> <em>'+serviceid[i]+'</em> <b>TID:</b> <em>'+tripid[i]+'</em></div>';
    }

    tripResults.innerHTML = tripstr;
    //console.log(tripstr);
}, function(error) {
  console.error("Failed!", error);
});

浏览器输出:

RID: SID: CT-16APR-Caltrain-Saturday-02 TID: 23a
RID: TaSj-16APR SID: CT-16APR-Caltrain-Saturday-02 TID: 25a
RID: SID: CT-16APR-Caltrain-Saturday-02 TID: 27a
RID: TaSj-16APR SID: CT-16APR-Caltrain-Saturday-02 TID: 01a
RID: SID: CT-16APR-Caltrain-Saturday-02 TID: 29a
RID: TaSj-16APR SID: CT-16APR-Caltrain-Saturday-02 TID: 31a
RID: SID: CT-16APR-Caltrain-Saturday-02 TID: 33a.................
RID: Lo-16APR SID: undefined TID: undefined
RID: SID: undefined TID: undefined
RID: Lo-16APR SID: undefined TID: undefined
RID: SID: undefined TID: undefined
RID: Lo-16APR SID: undefined TID: undefined
RID: SID: undefined TID: undefined
RID: Lo-16APR SID: undefined TID: undefined

我的输出正在跳过值,有时会打印出undefine。有没有更好的方法来获取每行文本文件的值( route_id,service_id,trip_id,trip_headsign,trip_short_name,direction_id,shape_id,wheelchair_accessible,bikes_allowed )?

0 个答案:

没有答案