1

I keep array localstorage. I want to delete an element from the array list on another page. My code is below but delete function does not work why?

push.js

existingEntries = JSON.parse(localStorage.getItem("allEntries"));
if (existingEntries == null) existingEntries = [];
entry = {
         "adi":path,
        };
localStorage.setItem("entry", JSON.stringify(entry));
existingEntries.push(entry);
localStorage.setItem("allEntries", JSON.stringify(existingEntries));

delete.js

delete: function (e) {
  var del = e.itemData.adi //The information of the clicked element is retrieved.
  for (var i = 0; i < dataSource.length; i++) {
    if (dataSource[i].adi == del) { dataSource.splice(i, 1); }
  }
  // insert the new stringified array into LocalStorage
  localStorage.getItem("allEntries") = 
  JSON.stringify(existingEntries);
}
onetwo12
  • 2,140
  • 5
  • 21
  • 32
Engineer23
  • 55
  • 1
  • 8

1 Answers1

0
var dataSource = JSON.parse(localStorage.dataSource); //whatever data you want
var del = e.itemData.adi //The information of the clicked element is retrieved.
for (var i = 0; i < dataSource.length; i++) {
 if(del === persons[i].name){  //look for match with name
    dataSource.splice(i, 1);
   break;  //exit loop since you found the person
 }
    localStorage.getItem("allEntries") = JSON.stringify(existingEntries); // insert the new stringified array into LocalStorage
}
hering
  • 1,916
  • 4
  • 28
  • 43
  • hi,The change only happens in delete.js. There is no general change – Engineer23 Aug 17 '17 at 15:25
  • i think this link will help you https://stackoverflow.com/questions/4201239/in-html5-is-the-localstorage-object-isolated-per-page-domain –  Aug 17 '17 at 17:44