【发布时间】:2020-06-08 06:28:59
【问题描述】:
我有这个代码
function deleteElement() {
const myArray = map(listItems, getText);
var elementToDelete =document.getElementById('deleteElement').value;
const index = myArray.findIndex((item) => item.includes(elementToDelete));
if (index > -1) {
// delete and update local storage
console.log("found element and index ", index);
let moment = localStorage.getItem('pelis_guardades');
let deleted = moment.splice(index, 1);
localStorage.setItem('pelis_guardades', JSON.stringify(deleted))
console.log(deleted);
}
}
我已经找到要删除的数组元素的索引,一切都很好,但现在我想“更新”本地存储以从索引中删除该项目。
我可以删除加载到本地存储中的数组上的特定值。称为 myArray。
const myArray = map(listItems, getText);
myArray 包含“原始字符串数据”,然后通过以下方式将其放入本地存储中,
localStorage.setItem('things',JSON.stringify(myArray));
如何从本地存储中删除?
我试过了,本地存储上的拼接方法不行!!
谢谢!
【问题讨论】:
标签: javascript html arrays local-storage