angular 2+ fetch specific value from local json file

MarcoLe

I want to fetch only a certain object from the file. But I somehow only can fetch the entire data from the file.

enter image description here

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.

vicbyte

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Fetch data from JSON local file in React

From Dev

Angular 2: file not found on local .json file

From Java

How to fetch data from local JSON file on react native?

From Dev

Fetch data from local file

From Dev

Fetch JSON data by specific value

From Dev

Fetch specific data from JSON

From Dev

How to fetch Angular JSON value

From Dev

How to fetch local json file into constructor in reactjs

From Dev

Fetch value from JSON in android

From Dev

How to fetch specific JSON data in Angular

From Dev

Fetch local path from current file in vim

From Dev

to fetch a specific value from resultset in Mule

From Dev

Consume Json file from Service in Angular 2

From Dev

Fetch the external json file through Angular JS

From Dev

Getting a specific value from an angular2 *ngFor array

From Dev

delete from a local json file

From Dev

delete from a local json file

From Dev

Fetch ID value from given JSON

From Dev

Not able to fetch the value of a key from JSON data

From Dev

unable to fetch data from json file in angularjs?

From Dev

Unable to fetch json file from android assets

From Dev

Python to fetch LDAP credentials from JSON file

From Dev

Fetch correct data from json file with postman

From Dev

Extracting specific value from a file

From Dev

Fetch specific input value

From Dev

Recursively parsing JSON via JSONObject to fetch value against specific keys

From Dev

Recursively parsing JSON via JSONObject to fetch value against specific keys

From Java

Angular 5 Service to read local .json file

From Dev

angular load local json file via services