如何从本地存储的数组中删除某些项目?

0 javascript arrays local-storage

我一直在开发一种工具来帮助人们在 GTA 中跟踪他们的汽车,但我不知道如何删除它们。

我尝试了多种方法,但无法让它发挥作用。

这是我的代码笔https://codepen.io/Tritangre97/pen/dyoyOKw?editors=1010

function deleteItem(index) {
  var existingEntries = JSON.parse(localStorage.getItem("allCars"));
  existingEntries.splice(0, index); // delete item at index
}
Run Code Online (Sandbox Code Playgroud)

use*_*217 5

Splice 不会更新本地存储,相反,一旦您删除了需要将新数组写回到本地存储的项目:

localStorage.setItem("allCars", existingEntries)
Run Code Online (Sandbox Code Playgroud)