Reputation: 2902
I have the following json file : api.json with the following 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
}
]
What I want to do is get an item from the object given the stop_name and arrival_name.
Let say:
var fromStop = "SAN FRANCISCO";
var toStop = "SAN ANTONIO";
How can I get the item with the above info from the object using pure javascript so I can be able to access the item index values.
Upvotes: 0
Views: 107
Reputation: 26191
There are two ways. The reduce
one gets you the indices of all matching objects and the findIndex
method gets you the first matching object's index.
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);
Upvotes: 0
Reputation: 27222
Try this :
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);
Output :
Fiddle demo : https://jsfiddle.net/zvdq4vLk/
Upvotes: 0
Reputation: 3124
You could just loop through the objects and find the 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);
Upvotes: 0
Reputation: 4150
Use the array's find method.
var toStop = "Footopia",
fromStop = "Barsville";
api.json.find(function(item) {return item.stop_name == fromStop && item.arrival_name == toStop;});
P.S.: I'm answering from the SO iPhone app so please forgive any formatting mistakes.
Upvotes: 1
Reputation: 36609
Use
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);
Upvotes: 1
Reputation: 1715
jsonOb is the parsed json object
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;
}
}
Upvotes: 0