es6 js实现数组循环

js yekong

只有值

let arr = ['ceshi', 'wanjun', 'wanjunshijie']
for (let val of arr) {
    console.log(val)
}
结果
ceshi
wanjun
wanjunshijie

只有下标

let arr = ['ceshi', 'wanjun', 'wanjunshijie']
for (let val of arr.keys()) {
    console.log(val)
}
结果
0
1
2

下标和值的数组

let arr = ['ceshi', 'wanjun', 'wanjunshijie']
for (let val of arr.entries()) {
    console.log(val)
}
结果
[0, 'ceshi']
[1, 'wanjun']
[2, 'wanjunshijie']

下标加值

let arr = ['ceshi', 'wanjun', 'wanjunshijie']
for (let [i, val] of arr.entries()) {
    console.log(i + ":" + val)
}
结果
0:ceshi
1:wanjun
2:wanjunshijie
喜欢