//创建数组对象
var list= [
{ id: 1, name: ‘小马‘},
{ id: 2, name: ‘小风‘ }
]
1.最喜欢用的(通用):
this.list.forEach((item,index)=>{
//数组遍历的对象item
console.log(item.name);
//数组遍历的下标
console.log(index);
})
2.查找对应数组的下标
//返回的是对应数组对象的下标
var index = list.findIndex(item => {
if (item.id == id) {
return true;
}
})
3.过滤查找返回对象的数组对象(查找单个)
var people= list.filter(item => {
if (item.name.includes(keywords)) {
return item
}
})
扩展循环对象属性:
var people={id:1,name:"wx"}
for(item in people){
var key=item;
var val=people[key];
console.log(key+":"+val);
//或者写法es6占位 `${key}:${val}`
}