数组的常用遍历方法常用有:for、map、forEach
let arr = [1,2,3,4,5];
arr.forEach((i)=>{
console.log(i);//1 2 3 4 5});
}
let arr = ['a','b','c','d'];
let newArr = arr.map((val,key)=>{
if(val=='c'){
return val;
}
});
console.log(newArr);//[undefined,undefined,'c',undefined];
let arr = [10, 20, 30, 40, 50];
for (let item in arr) {
console.log(item); // '0' '1' '2' '3' '4'}
}
如果是对象数据(可枚举属性)
let obj = { name:'xiaoming', age:15}
for(let item in obj){
console.log(item);//name age
}
【秉着学习的态度,如有错误请指正】