0

I have been struggling with this issue for the past two days, Please help me.

I have a json file having the below structure, and I am using jQuery getJSON to retrieve the json file:

{"locations":[
  {"gov": "Riyadh", "city": "Riyadh", "coordinates": [24.7135517, 46.67529569999999, 5.78], "img": "riyadh.jpg"},
  {"gov": "Dammam", "city": "Dammam", "coordinates": [26.340680925140543, 50.1088628919922, 5.81], "img": "dammam.jpg"},  
  {"gov": "Makkah", "city": "Makkah", "coordinates": [21.39988519210946, 39.8476324232422, 11], "img": "makkah.jpg"},  
  {"gov": "Makkah", "city": "Jeddah", "coordinates": [21.295001497070977, 39.2214117201172, 9], "img": "jeddah.jpg"
  }]
}

and I have the following JS:

function getJsonResults(p,prop=null) {
  var temp= [];
  var obj= {pro: prop};
  $.getJSON(p,obj, function(data) {
    var jsonResults= data.locations;
    var x = obj.pro;

    // loop through the results I got from some answers here in stackoverflow

    Object.keys(jsonResults).forEach(function(key) {
      //get the value of name
      var val = jsonResults[key][x];
      //push the name string in the array
      temp.push(val);
    });

    // this is how I did looping earlier
    // for(var i = 0; i< jsonResults.length; i++){

    //   if (prop==null) {
    //     temp.push(jsonResults[i]);
    //   }else{    
    //     temp.push(jsonResults[i][x]);
    //   }
    // } // end for
  });
  return temp;
}

var landsJson = 'assets/js/locations.json';
var govs= getJsonResults(landsJson,"gov"); 
var govsNoDup= ArrNoDupe(govs);

this works fine though when I check it in the console it doesn't look like the regular arrays i.e (3) [1,2,"k"] it shows like [] and when I try to use it as an argument for another function for example to remove duplicates it returns an empty array.

I am not much familiar with javascript but I feel the issue is that it returns like objects not regular array values of strings.

Kamal
  • 23
  • 7

0 Answers0