[英]angular 2+ fetch specific value from local json file
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. 我只想获取
lon an lan
输入相同的对象。
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. Res是一个数组,而不是单个对象,因此您需要一个循环。 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.