简体   繁体   中英

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:

  • 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.

It all relies on Storage.getItem() and Storage.setItem() methods.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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