①forEach()遍历数组
//遍历数组
arr.forEach(function(value,index,arr){
// 参数1 数组里面对应的每个值
// 参数2 数组里面的索引
// 参数3 原数组
// console.log(value)
// console.log(index)
console.log(arr)
console.log(arr[index])
})
②for循环遍历
for(let i=0; i<arr.length; i++){
console.log(arr[i]+"索引是"+i)
}
③while遍历
let i =0;
while(i<arr.length){
console.log(arr[i]+"索引是"+i)
i++
}
④do while遍历 (不建议使用)
let i=0
do{
console.log(arr[i]+"索引是"+i)
i++
}while(i<arr.length)
⑤for in 遍历(只能遍历对象)
for(let i in arr){
console.log(i)
console.log(arr[i])
}
⑥for of遍历(对象,数组都能遍历)
for(let i of arr){
console.log(i)
}
总结:for in 只能遍历对象
for of可以遍历对象跟数组
本文介绍了JavaScript中数组的多种遍历方式,如forEach()、for循环、while、do while、for in和for of,其中for in只能遍历对象,for of可遍历对象和数组。还介绍了新增的数组方法,包括将伪数组转数组、查找指定值、过滤元素等方法。
362

被折叠的 条评论
为什么被折叠?



