es6 entries(),keys() 和 values()

 1 for (let index of ['a', 'b'].keys()) {
 2   console.log(index);
 3 }
 4 // 0
 5 // 1
 6 
 7 for (let elem of ['a', 'b'].values()) {
 8   console.log(elem);
 9 }
10 // 'a'
11 // 'b'
12 
13 for (let [index, elem] of ['a', 'b'].entries()) {
14   console.log(index, elem);
15 }
16 // 0 "a"
17 // 1 "b"

参考地址:http://es6.ruanyifeng.com/#docs/array

ES6 提供三个新的方法——entries()keys()values()——用于遍历数组。它们都返回一个遍历器对象(详见《Iterator》一章),可以用for...of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历。

猜你喜欢

转载自www.cnblogs.com/zhaobao1830/p/8916203.html
今日推荐