javascript - 如何从 localStorage 数组中删除项目?我有
问题描述
我有一个存储在 localStorage 中的数组。如何删除数组的值并再次将其保存在 localStorage 中?
var cars = ["Saab", "Volvo", "BMW"];
localStorage.setItem("cars", JSON.stringify(cars));
let modifiedCars = JSON.parse(localStorage.getItem("cars")).splice(0, 1)
// How do I remove cars[0] from the array and store it back into localStorage?
解决方案
localStorage
是一个简单的基于字符串的存储。为了删除一个项目,你需要阅读它(就像你做的那样),删除它(就像你做的那样)并再次存储它:
localStorage.setItem('cars', JSON.stringify(modifiedCars));
推荐阅读
- laravel - 如何修复显示所有日期的日期过滤器。在我的日期过滤器中只显示今天和之后的日期而不是之前
- sql - Mongodb Qurie 获取时间序列记录
- reactjs - 我对这个 HOC(高阶组件)代码是否正确?
- javascript - 从 React App 上的 Google Picker 中的选定工作表访问数据
- http - Angular 7,如何为多个 HTTP 调用添加单个加载器
- javascript - 使用 javascript 创建交互式 3-D 地球仪
- git - 在詹金斯工作中动态选择git repo
- android - android.view.InflateException:错误膨胀类按钮
- javascript - 创建直接显示子行的数据表
- mysql - 使用 JSON_EXTRACT 时遇到路径问题