1. forEach:
|
等价于:
1 2 3 4 |
扫描二维码关注公众号,回复:
3045415 查看本文章
|
forEach方法中的function回调有三个参数:第一个参数是遍历的数组内容,第二个参数是对应的数组索引,第三个参数是数组本身
因此:
1 2 3 4 5 |
|
2. for in:
代码1:
var array = [1,2,3,4,5,6,7];
for(let index in array) {
console.log(index,array[index]);
}
结果1:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
代码2:
var A = {a:1,b:2,c:3,d:"hello world"};
for(let k in A) {
console.log(k,A[k]);
}
结果2:
a 1
b 2
c 3
d hello world