傻傻分不清的遍历

1、$.each()  jquery中的方法

遍历数组
$.each(arr, function(index, value){ console.log(value); })
1 遍历数组
2 $.each(obj, function(key, value){
3     console.log(key, value);
4 })

2、$(selector).each()  jquery中的方法

1 $("input[name='card']").each(function(index, item){
2     if($(this).prop("checked")){
3     // do something
4     }
5 })

3、arr.forEach()   ES5的方法,类似于常用的for循环

var arr = [1, 2, 3, 4];
arr.forEach(function(item, index, allarr){
    //do something
})

  

4、map()   ES5的方法  有返回值,注意和第3点的forEach区分

var arr=[1, 2, 3, 4];
var result = arr.map(function(item, index){
    return item*2
})
console.log(result); //[2,4,6,8]

  

 5、for in   ES5  遍历是索引index(key)

 6、for  of    ES6  遍历的是 value,可以中断,使用break, continue,return。可以遍历数组,对象、Set、Map、等(待补充)

 7、Arrary.prototype.filter()   ES6

 8、Arrary.prototype.some()   ES6

 9、Arrary.prototype.every()   ES6

 

猜你喜欢

转载自www.cnblogs.com/danchaofan123/p/11812077.html