$.each(obj, fn)


$.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 );   });  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值