如何从本地存储阵列中删除特定项目? [英] 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
的空数组. - 通过
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中的现有项目.
- To have an empty array of
notes
. - Add this array to
localStorage
vialocalStorage.setItem("savedNotes", JSON.stringify(notes))
. - Every time you add a note you will need to: parse this array back from
localStorage
withnotes = JSON.parse(localStorage.getItem("savedNotes"))
. - Then push the new
note
into this array vianotes.push(note)
. - Then set this item again with
localStorage.setItem("savedNotes", JSON.stringify(notes))
, it will update your existing item in the localStorage.
这一切都取决于 Storage.getItem( ) 和 存储.setItem() 方法.
It all relies on Storage.getItem() and Storage.setItem() methods.
要从阵列中删除note
,您需要执行相同的操作,希望您将在已分析的阵列中搜索此注释并将其删除.
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.
这篇关于如何从本地存储阵列中删除特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!