如何从本地存储阵列中删除特定项目? [英] How to remove specific item from local storage array?

查看:86
本文介绍了如何从本地存储阵列中删除特定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建待办事项列表",用户点击即可将新注释添加到列表中.

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 via localStorage.setItem("savedNotes", JSON.stringify(notes)).
  • Every time you add a note you will need to: parse this array back from localStorage with notes = JSON.parse(localStorage.getItem("savedNotes")).
  • Then push the new note into this array via notes.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆