为什么我的拼接方法会删除所有超过选定索引的对象?(javascript)
代码: 节点: 那么,考虑到我的todo.findIndex正在工作,我的todo.splice出了什么问题?我设置了一个todo.findIndex测试,以显示它只找到索引3。因此,在todo.splice函数中,todo.findIndex也应该等于3,对吗?我试图减少被删除的项目数量,甚至完全删除这些项目。我不知道我做错了什么为什么我的拼接方法会删除所有超过选定索引的对象?(javascript),javascript,node.js,Javascript,Node.js,代码: 节点: 那么,考虑到我的todo.findIndex正在工作,我的todo.splice出了什么问题?我设置了一个todo.findIndex测试,以显示它只找到索引3。因此,在todo.splice函数中,todo.findIndex也应该等于3,对吗?我试图减少被删除的项目数量,甚至完全删除这些项目。我不知道我做错了什么 3 [ { task: 'Free time', priority: 0 }, { task: 'Look for jobs', priority: 3 }
3
[
{ task: 'Free time', priority: 0 },
{ task: 'Look for jobs', priority: 3 },
{ task: 'Have a healthy relationship', priority: 2 }
]
[
{ task: 'Learn JS', priority: 1 },
{ task: 'Make dinner', priority: 0 },
{ task: 'Get sleep', priority: 2 }
]
1(要删除的项目数)添加到findIndex,而不是拼接。应该是这样。1(要删除的项目数)添加到findIndex,而不是splice。应该是这样。
3
[
{ task: 'Free time', priority: 0 },
{ task: 'Look for jobs', priority: 3 },
{ task: 'Have a healthy relationship', priority: 2 }
]
[
{ task: 'Learn JS', priority: 1 },
{ task: 'Make dinner', priority: 0 },
{ task: 'Get sleep', priority: 2 }
]
// My delete function
const deleteTodo = function (obj, title){
return obj.splice(obj.findIndex(function (obj){
return obj.task === title
}), 1)
}