繁体   English   中英

如何从本地存储阵列中删除特定项目?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM