Javascript 删除函数从数组中删除错误的项
因此,我正在制作我的待办事项应用程序,我遇到了一个奇怪的问题,我的removeToDo函数以奇怪的模式将错误的项目作为目标,除了第一个删除的项目(总是很好地删除)。假设数组中有id为0到6的项:Javascript 删除函数从数组中删除错误的项,javascript,html,css,arrays,indexing,Javascript,Html,Css,Arrays,Indexing,因此,我正在制作我的待办事项应用程序,我遇到了一个奇怪的问题,我的removeToDo函数以奇怪的模式将错误的项目作为目标,除了第一个删除的项目(总是很好地删除)。假设数组中有id为0到6的项: Clicked to remove item with ID = 3 - removed item with ID = 3 Clicked to remove item with ID = 4 - removed item with ID = 5 Clicked to remove item with
Clicked to remove item with ID = 3 - removed item with ID = 3
Clicked to remove item with ID = 4 - removed item with ID = 5
Clicked to remove item with ID = 5 - removed item with ID = 6
Clicked to remove item with ID = 0 - removed item with ID = 0
Clicked to remove item with ID = 2 - removed item with ID = 4
Clicked to remove item with ID = 1 - removed item with ID = 2
Clicked to remove item with ID = 6 - removed item with ID = 1
所以它实际上并没有遵循一个明显的模式(虽然它可能是id+1之类的东西,但看起来不是这样)。我第二次做了与上面完全相同的测试,看看它是否随机化,结果是否完全相同
这里有一些代码
HTML
JS
const list=document.getElementById(“list”);
常量输入=document.getElementById(“itemInput”);
让我看看你的身份证;
//从本地存储中获取项目
让data=localStorage.getItem('TODO');
//检查数据是否为空
如果(数据){
LIST=JSON.parse(数据)
id=LIST.length;//将列表id设置为数组中的最后一个
loadList(LIST);//将数组中的所有项加载到UI
}否则{
//如果数据为空
列表=[];
id=0;
}
函数加载列表(数组){
array.forEach(项=>{
addToDo(item.name、item.id、item.done、item.trash);
})
}
函数addToDo(toDo、id、done、trash){
//如果trash为true,则不要执行下面的代码
if(垃圾){return;}
const DONE=DONE?勾选:取消勾选;
常量行=完成?行通过:“”;
常量文本=`
${toDo}
`;
const position=“beforeend”;
list.insertAdjacentHTML(位置,文本);
}
//删除待办事项
函数removeToDo(元素,i){
让newList=[…LIST]
element.parentNode.parentNode.removeChild(element.parentNode);
i=newList.indexOf(newList[event.target.id])//{
常量元素=e.目标;
如果(e.target.className==“fas fa trash alt”){
移除(元素);
}else if(e.target.className==“far fa circle”){
作业完成(要素);
}else if(e.target.className==“远fa检查圆”){
作业完成(要素);
}
}
)
//使用“回车”键添加任务
document.addEventListener(“键控”,事件)=>{
如果(event.keyCode==13){
const toDo=input.value;
如果(待办事项){
addToDo(toDo,id,false,false);
LIST.push(
{
姓名:toDo,
id:id,
完成:错误,
垃圾:错
}
);
setItem('TODO',JSON.stringify(LIST));
id++;
input.value='';
}
}
})
编辑:
在奇怪的模式中删除的项目在列表数组中,我点击的实际按钮被删除了。我想我没有解释得足够好 我认为最好不要考虑删除项目的ID。 你也可以考虑它的价值。 请尝试使用
newList = newList.filter(function( obj ) {
return obj.name !== element.previousElementSibling.innerHTML;
});
我认为最好不要考虑删除物品的ID。 你也可以考虑它的价值。 请尝试使用
newList = newList.filter(function( obj ) {
return obj.name !== element.previousElementSibling.innerHTML;
});
问题是:
id = LIST.length; // set the list id to the last in the array
数组的.length
属性返回数组中的项数,但数组索引是从零开始的。数组中有5项(length==5
)的最后一项索引将是4
数组中最后一项的索引是.length-1
问题在于:
id = LIST.length; // set the list id to the last in the array
数组的.length
属性返回数组中的项数,但数组索引是从零开始的。数组中有5项(length==5
)的最后一项索引将是4
数组中最后一项的索引是
.length-1
而不是元素.parentNode.parentNode.removeChild(元素.parentNode);
,当您有正确的元素应该取出时,只需使用元素.remove()
。它只会删除bin图标,这就是它存在的原因。元素.parentNode.remove()
可以删除它,但原始问题仍然存在。请显示一个您将获得的localStorage
字符串示例。@ScottMarcus Storage TODO:“[{”name:“1”,“id”:0,“done”:false,“trash”:false},{”name:“2”,“id”:1,“done”:false,“trash”:false,{”name:“3”,“id”:2,“done”:false,“trash”:false,{”name:“4”,“id”“:3,“完成”:false,“垃圾桶”:false},{“名称”:“5”,“id”:“4”,“完成”:false,“垃圾桶”:false},{“名称”:“7”,“id”:“6”,“完成”:false,“垃圾桶”:false},{“名称”:“8”,“id”:“7”,“完成”:false,“垃圾桶”:false}]“长度:1_uuuuproto_uuu:storage而不是element.parentNode.parentNode.removeChild(element.parentNode);
,只要使用element.remove()
,当您有正确的应该取出的元素时。它只会删除bin图标,这就是它存在的原因。element.parentNode.remove()
可以删除它,但原始问题仍然存在。请显示一个您将获得的localStorage
字符串示例。@ScottMarcus Storage TODO:“[{”name:“1”,“id”:0,“done”:false,“trash”:false},{”name:“2”,“id”:1,“done”:false,“trash”:false,{”name:“3”,“id”:2,“done”:false,“trash”:false},{”name:“4”,“id”:3,“完成”:false,“垃圾”:false},{“名称”:“5”,“id”:4,“完成”:false,“垃圾”:false},{“名称”:“6”,“id”:5,“完成”:false,“垃圾”:false},{“名称”:“7”,“名称”:“8”,“id”:7,“完成”:false,“垃圾”:false}]“长度:1_uuuuuuuuuuuuuuuuuuuuuuuu原型:存储我尝试过,不幸的是它没有解决问题:(@PawelKrzesinski它仍然是一个问题,应该得到纠正。我尝试了它,但不幸的是它没有解决问题:(@PawelKrzesinski它仍然是一个问题,应该得到纠正。好的,到目前为止,这似乎是有效的,但您可以