
for-of:
fruits = ['banana','apple','peach','orange','mango','guava','water-melon'];
for(const item of fruits){
console.log(item);
}
'banana'
'apple'
'peach'
'orange'
'mango'
'guava'
'water-melon'
- If an array if looped over in the form of array.entries(), then the result will be each element in form of an array with index : value.
for(const item of fruits.entries()){
console.log(item);
}
[ 0, 'banana' ]
[ 1, 'apple' ]
[ 2, 'peach' ]
[ 3, 'orange' ]
[ 4, 'mango' ]
[ 5, 'guava' ]
[ 6, 'water-melon' ]
// Transform it into a single array comprising of sub-arrays:
fruits.entries(); // Object [Array Iterator] {}
[...fruits.entries()];
// [ [ 0, 'banana' ], [ 1, 'apple' ], [ 2, 'peach' ], [ 3, 'orange' ], [ 4, 'mango' ], [ 5, 'guava' ], [ 6, 'water-melon' ] ]
// Transform into a single array using for-of loop:
-> Method 1
for(const item of fruits.entries()){
console.log(`${item[0] + 1} : ${item[1]}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'
-> Method 2
for(const [i,el] of fruits.entries()){
console.log(`${i + 1} : ${el}`);
}
// '1 : banana' '2 : apple' '3 : peach' '4 : orange' '5 : mango' '6 : guava' '7 : water-melon'
以上就是迭代语句即for-of循环的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号