数组示例
let users = [ {“name”:“张三”,“age”: 10},
{“name”:“李四”,age: 14},
{“name”:“王五”,age: 9},
…
]
要求统计出数组里年龄大于10的人数。
方法一:使用filter和length
const result = users.filter(u => u.aage > 10 ).length;
方法二:使用reduce
const result = users.reduce((c, u) => u + (u.age > 10), 0)
checkItem(id) {
this.todos.forEach((item) => {
if (item.id === id) item.done = !item.done;
});
},
methods: {
//添加一个todo
addTodo(todoObj){
this.todos.unshift(todoObj)
},
//勾选or取消勾选一个todo
checkTodo(id){
this.todos.forEach((todo)=>{
if(todo.id === id) todo.done = !todo.done
})
},
//删除一个todo
deleteTodo(id){
this.todos = this.todos.filter( todo => todo.id !== id )
},
//全选or取消全选
checkAllTodo(done){
this.todos.forEach((todo)=>{
todo.done = done
})
},
//清除所有已经完成的todo
clearAllTodo(){
this.todos = this.todos.filter((todo)=>{
return !todo.done
})
}
},