Reputation: 59
I want to loop through this JSON file (structure below) and get all Hotels where country is Austria for example. Using getJson()
so am unable to change anything in the JSON file too.
Any help would be greatly appreciated.
[
{
"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"
}
]
Upvotes: 0
Views: 83
Reputation: 27242
Try Array map() and filter() method :
var json = [{
"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 austriaHotels = json.filter(function(item) {
return item.Country == 'Austria';
});
var hotelsName = austriaHotels.map(function(item) {
return item.Hotels;
});
console.log(hotelsName);
Upvotes: 0
Reputation: 2474
I don't know what exactly you want to do, but here's a working example that loops the json and checks if the hotel is in Austria and logs the name and city into the console:
var json = [{
"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"
}];
$(json).each(function () {
if (this.Country === "Austria") {
console.log("Found hotel " + this.Hotels + " in " + this.Stadt);
}
});
Upvotes: 2
Reputation: 4024
You'll need to store your locations, loop over them and group together the matching locations into a new array.
var locations = [{
"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"
}];
// Store the matched hotels here
var matches = [];
// Loop over hotels
for( i=0; i<locations.length; i++ ) {
// Check if the location is in Austria, if so push it to our matches array
if( locations[i].Country == 'Austria' ) {
matches.push(locations[i]);
}
}
// Check for matched hotels
console.log( matches );
Upvotes: -1