从javascript对象

时间:2016-06-13 03:45:21

标签: javascript arrays json object

我有以下json文件:api.json,带有以下json:

[
  {
    "stop_ids": 70021,
    "stop_name": "22ND ST STATION",
    "stop_lat": 37.757692,
    "stop_lon": -122.392318,
    "trip_id": 101,
    "departure_time": "08:20:00",
    "arrival_time": "09:13:00",
    "arrival_name": "ATHERTON STATION",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70022,
    "stop_name": "ATHERTON STATION",
    "stop_lat": 37.464458,
    "stop_lon": -122.198152,
    "trip_id": 102,
    "departure_time": "07:37:00",
    "arrival_time": "08:30:00",
    "arrival_name": "22ND ST STATION",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70023,
    "stop_name": "BAYSHORE STATION",
    "stop_lat": 37.711202,
    "stop_lon": -122.401366,
    "trip_id": 103,
    "departure_time": "12:31:00",
    "arrival_time": "12:41:00",
    "arrival_name": "SAN FRANCISCO",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70024,
    "stop_name": "SAN FRANCISCO",
    "stop_lat": 37.776541,
    "stop_lon": -122.395406,
    "trip_id": 104,
    "departure_time": "12:00:00",
    "arrival_time": "01:06:00",
    "arrival_name": "SAN ANTONIO",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  }
  ]

我想要做的是从给定stop_name和arrival_name的对象中获取一个项目。

让我们说:

var fromStop = "SAN FRANCISCO";
var toStop = "SAN ANTONIO";

如何使用纯javascript从对象获取带有上述信息的项目,以便我能够访问项目索引值。

6 个答案:

答案 0 :(得分:1)

  

使用Array#filter

input.filter(function(item) {
  return item.stop_name === fromStop && item.arrival_name === toStop;
});



var input = [{
  "stop_ids": 70021,
  "stop_name": "22ND ST STATION",
  "stop_lat": 37.757692,
  "stop_lon": -122.392318,
  "trip_id": 101,
  "departure_time": "08:20:00",
  "arrival_time": "09:13:00",
  "arrival_name": "ATHERTON STATION",
  "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
  "zone_id": 3329
}, {
  "stop_ids": 70022,
  "stop_name": "ATHERTON STATION",
  "stop_lat": 37.464458,
  "stop_lon": -122.198152,
  "trip_id": 102,
  "departure_time": "07:37:00",
  "arrival_time": "08:30:00",
  "arrival_name": "22ND ST STATION",
  "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
  "zone_id": 3329
}, {
  "stop_ids": 70023,
  "stop_name": "BAYSHORE STATION",
  "stop_lat": 37.711202,
  "stop_lon": -122.401366,
  "trip_id": 103,
  "departure_time": "12:31:00",
  "arrival_time": "12:41:00",
  "arrival_name": "SAN FRANCISCO",
  "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
  "zone_id": 3329
}, {
  "stop_ids": 70024,
  "stop_name": "SAN FRANCISCO",
  "stop_lat": 37.776541,
  "stop_lon": -122.395406,
  "trip_id": 104,
  "departure_time": "12:00:00",
  "arrival_time": "01:06:00",
  "arrival_name": "SAN ANTONIO",
  "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
  "zone_id": 3329
}];
var fromStop = "SAN FRANCISCO";
var toStop = "SAN ANTONIO";
var filtered = input.filter(function(item) {
  return item.stop_name === fromStop && item.arrival_name === toStop;
});
console.log(filtered);




答案 1 :(得分:1)

使用数组的查找方法。

var toStop = "Footopia",
       fromStop = "Barsville";
api.json.find(function(item) {return item.stop_name == fromStop && item.arrival_name == toStop;});

P.S。:我从SO iPhone应用程序回答,请原谅任何格式错误。

答案 2 :(得分:0)

jsonOb是解析的json对象

var fromStop = "SAN FRANCISCO";
  var toStop = "SAN ANTONIO";
  var result = {};
  for(i=0;i<jsonOb.length;i++)
  {
        if(fromStop == jsonOb[i].stop_name && toStop == jsonOb[i].arrival_name)
        {
            result = jsonOb[i];
            break;
        }
  }

答案 3 :(得分:0)

您可以循环浏览对象并找到matche

  var result;
  var fromStop = "SAN FRANCISCO";
  var toStop = "SAN ANTONIO";
  for (var i=0 ; i < obj.length ; i++){

      if (obj[i]['stop_name'] == fromStop && obj[i]['arrival_name'] == toStop ) {
          result = "The index is : "+i;
          break;
      }
  }
console.log(result);

答案 4 :(得分:0)

试试这个:

function getObjectFromList(fromStop, toStop, fromStopValue, toStopValue,list){
    return list.filter(function (item) {
        return ((item[fromStop] === fromStopValue) && (item[toStop] === toStopValue));
    });
};

var a = getObjectFromList("stop_name", "arrival_name", "SAN FRANCISCO", "SAN ANTONIO", obj);

console.log(a);

输出

enter image description here

小提琴演示: https://jsfiddle.net/zvdq4vLk/

答案 5 :(得分:0)

有两种方法。 reduce获取所有匹配对象的索引,findIndex方法获取第一个匹配对象的索引。

var data = [
  {
    "stop_ids": 70021,
    "stop_name": "22ND ST STATION",
    "stop_lat": 37.757692,
    "stop_lon": -122.392318,
    "trip_id": 101,
    "departure_time": "08:20:00",
    "arrival_time": "09:13:00",
    "arrival_name": "ATHERTON STATION",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70022,
    "stop_name": "ATHERTON STATION",
    "stop_lat": 37.464458,
    "stop_lon": -122.198152,
    "trip_id": 102,
    "departure_time": "07:37:00",
    "arrival_time": "08:30:00",
    "arrival_name": "22ND ST STATION",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70023,
    "stop_name": "BAYSHORE STATION",
    "stop_lat": 37.711202,
    "stop_lon": -122.401366,
    "trip_id": 103,
    "departure_time": "12:31:00",
    "arrival_time": "12:41:00",
    "arrival_name": "SAN FRANCISCO",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  },
  {
    "stop_ids": 70024,
    "stop_name": "SAN FRANCISCO",
    "stop_lat": 37.776541,
    "stop_lon": -122.395406,
    "trip_id": 104,
    "departure_time": "12:00:00",
    "arrival_time": "01:06:00",
    "arrival_name": "SAN ANTONIO",
    "train_name": ["sbs local", "Shuttle Mak", "beast tar"],
    "zone_id": 3329
  }
  ],
  indices = data.reduce((p,c,i) => c.stop_name == "SAN FRANCISCO" && c.arrival_name == "SAN ANTONIO" ? p.concat(i):p,[]),
    index = data.findIndex(e => e.stop_name == "SAN FRANCISCO" && e.arrival_name == "SAN ANTONIO");
console.log(indices);
console.log(index);