Looping through an associative array in JavaScript is straight forward, but it does have some catches. You will want to use a for (...in...) loop. This will iterate through every index in the array and even the properties of the array. In order to loop through only the elements within the array, you’ll want to do something similar to this:
for (var name in associativeArray) {
if (name != "length") {
// Code that interacts with associativeArray[name]
// name will contain the index for each property
}
}
The above code snippet can also be useful for iterating through the properties within an object. Firefox supports iterating through an array using the each keyword like so:
for each (var object in associativeArray) {
// object now contains the actual item and can be directly used.
}
In this case, you do not need to worry about picking up the length property.