首页 > 其他 > 详细

傻傻分不清的遍历

时间:2019-11-08 09:38:18      阅读:85      评论:0      收藏:0      [点我收藏+]

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

 

傻傻分不清的遍历

原文:https://www.cnblogs.com/danchaofan123/p/11812077.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!