繁体   English   中英

更新 localstorage object 值并使用 jquery 保存

[英]Update localstorage object value & save it using jquery

如何更新 localstorage object 值并使用 jquery 保存它?

下面是我试过的代码

var cachekey = "shop/elasticCache/sku$$" + this.product.parentSku
const { category } = JSON.parse(
  localStorage.getItem("shop/elasticCache/sku$$" + this.product.parentSku)
)
var productcache = []
productcache = localStorage.getItem(cachekey)
var updatedproductcache = JSON.parse(productcache)
updatedproductcache.name = category
productcache = JSON.stringify(updatedproductcache)
localStorage.setItem(cachekey, productcache)

我正在尝试从 localstorage json 更新category数据

如果您删除不必要的代码(重复代码),您将只有这个

const cachekey = "shop/elasticCache/sku$$" + this.product.parentSku,
  {category} = JSON.parse(localStorage.getItem(cachekey));
category.name = category;
localStorage.setItem(cachekey, JSON.stringify(category));

虽然它没有意义,但它应该可以正常工作,你需要检查typeof category !== "undefined" ,也许它在你的 localStorage 中还不存在

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM