how to search for a value in different Json files

what i trying to do is when a user enters the longtitude and latitude of a location, the application returns the associated grid code which is stored in one of the 3 json data files. i am trying to search in all three files at the same time this is my try but smees not working.

  async function lookupGridcodeByLatAndLong(lat, long){     const response=await fetch('data/GHI_part01.json');     const line=await response.json();      var result = line.find(x => x.lat == lat && x.long == long);        if (result) {       return result.GRIDCODE;        } else {       lookupGHI_part02(lat, long);        }  } async function lookupGHI_part02(lat, long){     const response=await fetch('data/GHI_part02.json');     const line=await response.json();     var result = line.find(x => x.lat == lat && x.long == long);        if (result) {       return result.GRIDCODE;        } else {       lookupGHI_part03(lat, long);        } } async function lookupGHI_part03(lat, long){     const response=await fetch('data/GHI_part03.json');     const line=await response.json();      var result = line.find(x => x.lat == lat && x.long == long);        if (result) {       return result.GRIDCODE;        } else {       return new Error('No records found');        } } $("#Btn" ).click(function() {     lookupGridcodeByLatAndLong(lat, long)     .then(gridcode => {     var ghi = gridcode;}) $("#result").show(); $("#resultat").html(ghi); } 

this how my data look like

[{"GRIDCODE":2115,"Lat":29.478,"Long":-5.565},{"GRIDCODE":2115,"Lat":29.478,"Long":-5.562},{"GRIDCODE":2115,"Lat":29.486,"Long":-5.572},{"GRIDCODE":2113,"Lat":29.483,"Long":-5.567},{"GRIDCODE":2109,"Lat":29.483,"Long":-5.559},{"GRIDCODE":2115,"Lat":29.485,"Long":-5.554},{"GRIDCODE":2115,"Lat":29.493,"Long":-5.616},{"GRIDCODE":2120,"Lat":29.492,"Long":-5.609},{"GRIDCODE":2116,"Lat":29.492,"Long":-5.601},{"GRIDCODE":2116,"Lat":29.492,"Long":-5.592},{"GRIDCODE":2115,"Lat":29.493,"Long":-5.584},{"GRIDCODE":2110,"Lat":29.491,"Long":-5.575}] 
Professor Asked on October 26, 2020 in Android langue.
Add Comment
1 Answer(s)

first error was in

var result = line.find(x => x.Lat == lat && x.Long == long); 

I changed to capital “L”s in x.Lat and x.Long

scecond inseted of calling function inside another funcution i change it to this

    async function lookupGridcodeByLatAndLong(lat, long){     const response=await fetch('data/GHI_part01.json');     const line=await response.json();     var check =0;     var result = line.find(x => x.Lat == lat && x.Long == long);        if (result) {       return result.GRIDCODE;        }              else {        var check = 1;        }              if (check == 1)              {                 const response=await fetch('data/GHI_part02.json');                 const line=await response.json();                  var result = line.find(x => x.Lat == lat && x.Long == long);                    if (result) {                   return result.GRIDCODE;                    } else {                     check = 2                    }              }              if (check = 2)              {                  const response=await fetch('data/GHI_part03.json');                 const line=await response.json();                  var result = line.find(x => x.Lat == lat && x.Long == long);                    if (result) {                   return result.GRIDCODE;                    } else {                   return new Error('No records found');                    }              } } 
Train Answered on October 26, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.