I want to fetch only a certain object from the file. But I somehow only can fetch the entire data from the file.
So far my method to fetch the data looks like this:
getGeoCityFromLocalFile(lon, lat) {
return this.http.get('/assets/city-list.json')
.map(res => res.json()).subscribe(res => {
If(res.coord.lon === lon && res.coord.lat === lat) {
this.cityStatsStorage = res;
console.log(this.cityStatsStorage);
return this.cityStatsStorage;
}
});
}
I want to fetch only this Object where the lon an lan
Input are the same.
Your problem lies in here:
If(res.coord.lon === lon && res.coord.lat === lat)
Res is an array and not a single object, so you would need a loop. Example:
for (var i=0; i<res.length; i++){
if (res[i].coord.lon === lon && res[i].coord.lat === lat){
this.cityStatsStorage = res[i];
console.log(this.cityStatsStorage);
return this.cityStatsStorage;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments