簡體   English   中英

angular 2+從本地json文件中獲取特定值

[英]angular 2+ fetch specific value from local json file

我只想從文件中獲取某個對象。 但是我只能以某種方式從文件中獲取整個數據。

在此處輸入圖片說明

到目前為止,我獲取數據的方法如下所示:

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;
         }
    });
}

我只想獲取lon an lan輸入相同的對象。

您的問題出在這里:

If(res.coord.lon === lon && res.coord.lat === lat)

Res是一個數組,而不是單個對象,因此您需要一個循環。 例:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM