How jquery each() function works?

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.

In the case of an array, the callback is passed an array index and a corresponding array value each time.

$.each([ 52, 97 ], function( index, value ) {
  alert( index + ": " + value );
});

OUTPUT:

0: 52
1: 97

If an object is used as the collection, the callback is passed a key-value pair each time:

var obj = {
  "flammable": "inflammable",
  "duh": "no duh"
};
$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

OUTPUT:

flammable: inflammable
duh: no duh

NOTE: $.each() and $(selector).each() are two different methods defined in two different ways.

You can get more details about this topic from here.
To get to know more about other JQuery topics, you can check these articles too.

Please follow and like us:

Related Posts

Leave a Reply

Share