Iterating over jQuery and non | jquery foreach each
jQueryprovidesanobjectiteratorutilitycalled$.each()aswellasajQuerycollectioniterator:.each().Thesearenotinterchangeable.Inaddition,thereareacoupleofhelpfulmethodscalled$.map()and.map()thatcanshortcutoneofourcommoniterationusecases.link$.each()[1]$.each()[2]isagenericiteratorfunctionforloopingoverobject,arrays,andarray-likeobjects.Plainobjectsareiteratedviatheirnamedpropertieswhilearraysandarray-likeobjectsareiteratedviatheirindices.$.each()isessentiallyadrop-inreplacementofatraditionalfororf...
jQuery provides an object iterator utility called $.each() as well as a jQuery collection iterator: .each(). These are not interchangeable. In addition, there are a couple of helpful methods called $.map() and .map() that can shortcut one of our common iteration use cases.
link $.each()[1]$.each()[2] is a generic iterator function for looping over object, arrays, and array-like objects. Plain objects are iterated via their named properties while arrays and array-like objects are iterated via their indices.
$.each() is essentially a drop-in replacement of a traditional for or for-in loop. Given:
1
2
3
var arr = [ 1, 2, 3, 4, 5 ];
Then this:
1
2
3
4
5
for ( var i = 0, l = arr.length; i < l; i++ ) {
Can be replac...