如何删除数组中的localstroage

时间:2017-08-17 11:55:30

标签: javascript arrays local-storage

我保持数组localstorage。我想从另一个页面上的数组列表中删除一个元素。我的代码在下面,但删除功能不起作用?

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);
}

1 个答案:

答案 0 :(得分: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
}