$.each(obj, fn)

一个通用的迭代函数,可用于近似地迭代对象和数组。

这个函数与$().each()不同,$().each()是专门用于迭代和执行jQuery对象的函数。而这个函数可以用于迭代任何对象和数组。 这个函数的回调中包含两个参数:第一个是key(对象)或index(数组),第二个是值。

返回值:Object

参数:

  • obj (Object): 要重复迭代的对象或数组
  • fn (Function): 要在每个对象中执行的函数
 
示例:

这是一个迭代数组中所有项目的例子,通过函数访问了其中的每个项目和索引。

$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n ); });

示例:

这是一个迭代对象中所有属性的例子,通过函数访问了每个属性的名称和值。

$.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n ); });

 
$.each( obj, fn )

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. 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. Other return values are ignored.

Return value: Object
Parameters:

  • obj (Object): The object, or array, to iterate over.
  • fn (Function): The function that will be executed on every object.
 

Example:

This is an example of iterating over the items in an array, accessing both the current item and its index.

 $.each( [0,1,2], function(i, n){     alert( "Item #" + i + ": " + n );   });  

Example:

This is an example of iterating over the properties in an Object, accessing both the current item and its key.

 $.each( { name: "John", lang: "JS" }, function(i, n){     alert( "Name: " + i + ", Value: " + n );   });