ES5 array traversal methods
1, for loop
const arr = [1, 2, 3, 4, 5] for (let i = 0; i < arr.length; i++) { console.log(arr[i]) }
2、forEach
const arr = [1, 2, 3, 4, 5] arr.forEach(function (item) { console.log(item) })
The difference between the for loop: You can not use the break and continue
3、every
const arr = [1, 2, 3, 4, 5] arr.every(function (item) { console.log(item) return true })
The distinction for loop: break and can continue, but it must return false, respectively, and return true alternative
4、for in
const arr = [1, 2, 3, 4, 5] for (let index in arr) { console.log(arr[index]) }
① for in the design of the object is to be traversed to get a string type of keys, so strictly speaking, does not apply to array traversal
② for in support of break and continue, but in the judgment to use "==" do not use "===", because here is the string index
ES6 array traversal methods
5、for of
const arr = [1, 2, 3, 4, 5] for (let item of arr) { console.log(item) }
In addition to the array can traverse, since the object definition structure