for…in
const arr = [1, 2, 3, [4, 5, [6, [1]]], 7]
let flatten = function (arr) {
for(let i in arr){
//console.log(arr[i]);
console.log(i);
}
}
console.log(flatten(arr));
数组下标:
const arr = [1, 2, 3, [4, 5, [6, [1]]], 7]
let flatten = function (arr) {
for(let i in arr){
console.log(arr[i]);
//console.log(i);
}
}
console.log(flatten(arr));
数组值:
for…of
const arr = [1, 2, 3, [4, 5, [6, [1]]], 7]
let flatten = function (arr) {
for(let j of arr){
console.log(j);
}
}
console.log(flatten(arr));
数组值:
forEach
const arr = [1, 2, 3, [4, 5, [6, [1]]], 7]
let flatten = function (arr) {
arr.forEach((item,index) => {
console.log(item,index);
})
}
console.log(flatten(arr));