我不知道如何完成这个功能。我已经过滤找到与 id 匹配的变量,但现在我需要将该项目与 delItem var 匹配并将其删除。
function deleteToDo(tot) {
let delItem = toDos.filter((remove) => remove.id === tot);
/// i need to remove item from toDos array that matches delItem.
renderTheUI(toDos);
}
最佳答案
Array#filter 返回一个数组,其中包含与谓词匹配的所有项目。不是获取要删除的项目,而是通过检查 id
是否不等于 tot
来获取不包含该项目的数组。然后在renderTheUI
中使用过滤后的数组:
function deleteToDo(tot) {
const filteredToDos = toDos.filter((item) => item.id !== tot);
renderTheUI(filteredToDos);
}
我建议将对 renderTheUI
的调用移出 deleteToDo
方法,因为它可能会非常困惑。 deleteToDo
将返回更新后的数组,然后您可以渲染新数组:
function deleteToDo(tot) {
return toDos.filter((item) => item.id !== tot);
}
const filteredToDos = deleteToDo(2);
renderTheUI(filteredToDos);
关于javascript - 需要从 toDos 数组中删除 delItem 不知道如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47703592/