Javascript forEach()跳过项目

时间:2018-12-12 21:59:38

标签: javascript arrays foreach

我是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)

1 个答案:

答案 0 :(得分:1)

您可以改用Array.prototype.filter

const undone = todo.filter((item) => !item.completed)
const completed = todo.filter((item) => item.completed)