jQuery.each( array, callback )Returns: Object
Description: 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.
-
version added: 1.0jQuery.each( array, callback )
-
arrayType: ArrayLikeObjectThe array or array-like object to iterate over.
-
callbackThe function that will be executed on every value.
-
-
version added: 1.0jQuery.each( object, callback )
-
objectType: ObjectThe object to iterate over.
-
callbackThe function that will be executed on every value.
-
$.each()
function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each()
function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this
keyword, but Javascript will always wrap the this
value as an Object
even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.
1
2
3
|
|
1: 97
1
2
3
4
5
6
7
|
|
duh: no duh
$.each()
loop at a particular iteration by making the callback function return false
. Returning non-false is the same as a continue
statement in a for loop; it will skip immediately to the next iteration.
Examples:
Example 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
|
Demo:
Example 2
1
2
3
|
|
Example 3
1
2
3
|
|