0

OK, so I have a json structure as follows.

Basically, what I want to do is loop over the json file and pull out the data if Country is equal to a specific value.

I'm sure this is pretty easy but i just can't work it out.

    {
"Site ID": 19955,
"Hotels": "Ramada Salzburg City Centre",
"Stadt": "Salzburg",
"Country": "Austria",
"Region": "Central & Eastern Europe",
"Link DE": "",
"Link EN": "",
"Link TR": "",
"Lat": 47.8137521,
"Long": 13.044259,
"Image": "/Salzburg.jpg"
     }

4 Answers4

2

Use Array.prototype.filter

let austrianSites = sites.filter(site => site.Country === 'Austria')
Phil
  • 157,677
  • 23
  • 242
  • 245
2

Try

for (var i = 0, len = structure.length; i < len; i++) {
  if (structure[i].Country === someValue) {
    // do something here
  }
}
rfornal
  • 5,072
  • 5
  • 30
  • 42
0

How about checking if the JSON object has a property "Country" and if it does then output it. Hope it helps!

var jsonObject =  {
"Site ID": 19955,
"Hotels": "Ramada Salzburg City Centre",
"Stadt": "Salzburg",
"Country": "Austria",
"Region": "Central & Eastern Europe",
"Link DE": "",
"Link EN": "",
"Link TR": "",
"Lat": 47.8137521,
"Long": 13.044259,
"Image": "wp-content/themes/wyndham-hotels/img/Salzburg.jpg"
     }

for(var i in jsonObject){
  if(jsonObject.hasOwnProperty("Country")){
    var x = jsonObject.Country;
  }
}
document.write("The Country is: " + x);
HenryDev
  • 4,685
  • 5
  • 27
  • 64
0

Please try this following:

var places = [{
  "Site ID": 19955,
  "Hotels": "Ramada Salzburg City Centre",
  "Stadt": "Salzburg",
  "Country": "Austria",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}, {
  "Site ID": 1211,
  "Hotels": "test",
  "Stadt": "Salzburg",
  "Country": "NZ",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}]

var filtered = places.filter(function(p) {
  return p.Country === "NZ";
})
console.log(filtered);
RizkiDPrast
  • 1,695
  • 1
  • 14
  • 21