博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
j-3. foreach ,for of ,for in-------待续
阅读量:4580 次
发布时间:2019-06-09

本文共 722 字,大约阅读时间需要 2 分钟。

each()是jquery中的

each() 方法规定为每个匹配元素规定运行的函数。

提示:返回 false 可用于及早停止循环。

$(selector).each(function(index,element)) .each()是一个for循环的包装迭代器 .each()通过回调的方式处理,并且会有2个固定的实参,索引与元素(从0开始计数) .each()回调方法中的this指向当前迭代的dom元素  

for in

for in 遍历的实际是对象的属性名称也就是索引

index 索引为字符串,无法直接进行几何运算

遍历出来按照索引值大小排列打乱以前排序

所以一般只有不用它

 

 

a.name = "Hello";

for(x in a){
  console.log(x); //0,1,2,name
}

 
arr={3:6,1:4,2:5};
//   a.name = "Hello";
for(let index in arr){
  console.log(index,arr[index]); //1:4,2:5,3:6
}

 

for of (es6新特性)

它遍历的是素组内的元素

var array = [1,2,3,4,5,6,7];

 

 

for(let v of array) {

    console.log(v);
};

结果如下:

1
2
3
4
5
6
7

forEach()

 

array=[1,2,3,4,5,6]
array.forEach(v=>{
    console.log(v);
});
 
结果:1 2 3 4 5 6

 

转载于:https://www.cnblogs.com/stone5/p/9062797.html

你可能感兴趣的文章