JaveScript iterate method

JaveScript iterate method

The first: for loop

An array of values ​​for each traversal

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}

The second: for .... in 

Traversing the array in each of the key value (subscript)

 
 
let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let item in arr) {
console.log(item);
}

Third: for .... of

1:ES5   

An array of values ​​for each traversal

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let item of arr) {
    console.log(item);
}

2:ES6 

a: a traversal key for each value in the array (subscript) Keys ()

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let item of arr.keys()) {
    console.log(item);
}

b: the value of each traverse the values ​​array ()

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let item of arr. values()) {
    console.log(item);
}

c: a traversing each value in the array, and the key corresponding to the value of each (subscript) entries It ()

let arr = [1, 2, 3, 4, 5, 6, 7, 8];
for (let item of arr. entries()) {
console.log(item);
}

Guess you like

Origin www.cnblogs.com/zxq519896763/p/12184450.html