-
一般写法
for (let index = 0; index < arr.length; index++) { console.log(arr[index]) } -
for...in
for (let index in arr) { console.log(a[index]); }for in更适合遍历对象 不适合用于数组
- 此时的index是字符串数字
- 不一定按照数组内部的顺序输出
- 还会遍历数组的可枚举属性,包括原型方法
-
for...of
for (let value of arr) { console.log(value); }不能直接遍历对象
- for…of正常遍历,都需要实现一个遍历器Iterator(说到遍历器:也就是迭代器Iterator而Object对象本身没有内置的遍历器,并没有实现这个接口,使得它无法被for…of遍历。
另一循环写法forEach
```
//ES5的写法
myArray.forEach(function(index){
//操作你的index,index即为数组中的元素
})
```
\