[英]How to remove specific item from local storage array?
我正在构建“待办事项列表”,用户单击后,用户会将新注释添加到列表中。
我将所有附加数据保存在本地存储中。
现在,我想从阵列中删除单击的笔记,并将其保存回本地存储。 我有以下代码:
**//Here i get the note**
var getNote = JSON.parse(localStorage.getItem("savedNotes")) || [];
$("#notes-section").append(getNote);
**//Here i set the note**
getNote.push(note);
localStorage.setItem("savedNotes", JSON.stringify(getNote));
**//Here i want to remove the note from the array**
$(document).on('click', '.pin', function() {
$(this).parent().css({opacity: 1.0, visibility: "visible"}).animate({opacity: 0}, 2000);
for(var i =0 ; i < getNote.length; i++ ){
getNote.splice(i,1);
localStorage.setItem("savedNotes", JSON.stringify(getNote));
}
});
如评论中所述,您只需要提供相关代码,但只是为了清除此处的内容,方法如下:
notes
。 localStorage.setItem("savedNotes", JSON.stringify(notes))
将此数组添加到localStorage
。 notes = JSON.parse(localStorage.getItem("savedNotes"))
从localStorage
解析此数组。 notes.push(note)
将新note
推入此数组。 localStorage.setItem("savedNotes", JSON.stringify(notes))
再次设置该项目,它将更新您在localStorage中的现有项目。 所有这些都依赖于Storage.getItem()和Storage.setItem()方法。
要从阵列中删除note
,您需要执行相同的操作,希望您将在已解析的数组中搜索此注释并将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.