js 实现循环遍历数组


for in循环遍历

let arr = [1, 2, 3, 4, 4, 3],
    str = ''
for (const val in arr) {
    str += val + ' '
}
console.log(str); // 0 1 2 3 4 5 

 

 

for of循环遍历对象(数组)

let arr = [1, 2, 3, 4, 4, 3],
    str = ''
for (const val of arr) {
    str += val + ' '
}
console.log(str); // 1 2 3 4 4 3

 

forEach 循环数组

let arr = [1, 2]
arr.forEach((item, index) => {
    console.log(item,index);    // 1 0
                             // 2 1
});

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM