[英]How to remove specific item from local storage array?
I'm building a 'to do list' , The user appends new notes to the list after clicking . 我正在构建“待办事项列表”,用户单击后,用户会将新注释添加到列表中。
I saved all the appended data in the local storage. 我将所有附加数据保存在本地存储中。
Now i want to remove the clicked note from the array and save it back to the local storage. 现在,我想从阵列中删除单击的笔记,并将其保存回本地存储。 I have this code: 我有以下代码:
**//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));
}
});
As stated in comments you need to provide only relevant code, but just to clear things out here, the way to go here is: 如评论中所述,您只需要提供相关代码,但只是为了清除此处的内容,方法如下:
notes
. 空的notes
。 localStorage
via localStorage.setItem("savedNotes", JSON.stringify(notes))
. 通过localStorage.setItem("savedNotes", JSON.stringify(notes))
将此数组添加到localStorage
。 localStorage
with notes = JSON.parse(localStorage.getItem("savedNotes"))
. 每次添加注释时,您将需要:使用notes = JSON.parse(localStorage.getItem("savedNotes"))
从localStorage
解析此数组。 note
into this array via notes.push(note)
. 然后通过notes.push(note)
将新note
推入此数组。 localStorage.setItem("savedNotes", JSON.stringify(notes))
, it will update your existing item in the localStorage. 然后使用localStorage.setItem("savedNotes", JSON.stringify(notes))
再次设置该项目,它将更新您在localStorage中的现有项目。 It all relies on Storage.getItem() and Storage.setItem() methods. 所有这些都依赖于Storage.getItem()和Storage.setItem()方法。
And to remove a note
from the array you need to do the same thing, expect that you will search for this note in the parsed array and remove it. 要从阵列中删除note
,您需要执行相同的操作,希望您将在已解析的数组中搜索此注释并将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.