for-of循环

ES6为我们提供了for-in循环和for-Each循环还要强大

之前的for-in循环

var arr=['apple','orange','banana','strawberry'];
for (var i in arr) { 
  console.log(i);
}
//打印的是索引值
0,1,2,3

for-of循环

遍历数组

var arr=['apple','orange','banana','strawberry'];
for (var i in arr) { 
  console.log(i);
}
//打印的是每一项的值
apple
orange
banana
strawberry

也可以遍历字符串

var string = "boa";

for (var value of string) {
    console.log(value);
}

// "b"
// "o"
// "o"

遍历Map对象

let iterable = new Map([["a", 1], ["b", 2], ["c", 3]]);

for (let entry of iterable) {
  console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]

for (let [key, value] of iterable) {
  console.log(value);
}
// 1
// 2
// 3

你可能感兴趣的:(for-of循环)