A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
This function is not the same as $().each() - which is used to iterate, exclusively, over a jQuery object. This function can be used to iterate over anything.
The callback has two arguments:the key (objects) or index (arrays) as the first, and the value as the second.
If you wish to break the each() loop at a particular iteration you can do so by making your function return false. Returning non-false is the same as acontinue statement in a for loop, it will skip immediately to the next iteration.Arguments:
| object | Object | |
|---|---|---|
| The object or array to iterate over. | ||
| callback | Function | |
The function that will be executed on every object.
function callback(indexInArray, valueOfElement) {
var booleanKeepGoing;
this; // == valueOfElement (casted to Object)
return booleanKeepGoing; // optional, unless false
// and want to stop looping
}
| ||
