我是javascript新手。我只是想删除所有已完成的对象:true。
但是我的函数无法做到这一点。我想念什么?
const todo = [{
title: 'do this',
completed: true
},{
title: 'do this',
completed: true
},{
title: 'do this',
completed: true
},{
title: 'do this',
completed: true
}]
const remove = function(todo){
todo.forEach(function(item,index){
if(item.completed){
todo.splice(index,1)
}
})
}
remove(todo)
console.log(todo)
答案 0 :(得分:1)
您可以改用Array.prototype.filter
:
const undone = todo.filter((item) => !item.completed)
const completed = todo.filter((item) => item.completed)