简体   繁体   中英

jQuery loop through JSON objects

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"
  }
]

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);
  }
});

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); 

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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM