javascript中forEach,和jquery中each

forEach是ES5中操作数组的一种方法,主要功能是遍历数组

1.forEach方法中的function回调有三个参数:第一个参数是遍历的数组内容,第二个参数是对应的数组索引,第三个参数是数组本身

[].forEach(function(item,index,array){

  });

等价于

$.each([],function(index,item,array){

 })

2.each方法中的function回调有三个参数:第一个参数是对应的数组索引,第二个参数是遍历的数组内容,第三个参数是数组本身

在操作dom元素时的用法

$("li").each(function(index, item, array) {

$(item).click(function() {

//获取内容

console.log($(this).html())

//获取索引

console.log($(this).index())

})

})

3.map:map即是 “映射”的意思 用法与 forEach 相似,用法即: 

[].map(function(value,index,array){

})