How to Iterate Through an Array of Objects Using JavaScript

Suppose you have an array of objects.

var zipCodes = [
{"ZIP": 30001, "LAT": 30.750633, "LNG": -63.997177},    
{"ZIP": 10001, "LAT": 40.750633, "LNG": -73.997177 },
{"ZIP": 20001, "LAT": 50.750633, "LNG": -83.997177 }];

If you want to grab only the LAT and LNG values for a matching ZIP (as the key), you can easily use the for loop:

var key = 10001; //the zip code you want to search
var lng, lat;
for(var i=0; i < zipCodes.length; i++){
  if(zipCodes[i].ZIP == key){
    lat = zipCodes[i].LAT;
    lng = zipCodes[i].LNG;
    break;
   }
}

However, if you just want to loop through the entire array and print them out, you could use the object.forEach() method:

zipCodes.forEach(function(item,index){
  lat = item.LAT;
  lng = item.LNG;
});

You can try running the code here: