如何从本地存储中的数组中删除某些项目? [英] How to delete certain item from array in localstorage?

查看:64
本文介绍了如何从本地存储中的数组中删除某些项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发一种工具,以帮助人们在GTA中跟踪自己的汽车,但我不知道如何将其移除.

I've been working on a tool to help people keep track of their cars in GTA, but I can't figure out how to remove them.

我已经尝试了多种方法,但是无法正常工作.

I've tried multiple things, but can't get it to work.

这是我的代码笔 https://codepen.io/Tristangre97/pen/dyoyOKw?编辑者= 1010

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

推荐答案

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

Splice won't update the local storage, instead, once you have removed the items you need to write the new array back to the local storage:

localStorage.setItem("allCars", existingEntries)

这篇关于如何从本地存储中的数组中删除某些项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆