1.数组下标遍历
数组下标遍历是最常用也最普通的数组遍历方式
for (let index = 0; index < array.length; index++) {
const element = array[index];
}
例如:
let myarray = [1, 3, 5, 7, 9, 11];
// 数组下标遍历
for (let index = 0; index < myarray.length; index++) {
const element = myarray[index];
console.log(element);
}
2.for in遍历
for in是根据数组的键名来遍历
for (const key in newArray) {
console.log(newArray[key]);
}
例如:
let myarray = [1, 3, 5, 7, 9, 11];
for (const index in myarray) {
console.log(myarray[index]);
}
3.for-each遍历
用于调用数组的每个元素,并将元素传递给回调函数
let myarray = [1, 3, 5, 7, 9, 11];
myarray.forEach(function(value,index,arr){
console.log(value)
});










网友评论