简体   繁体   English

更新 localstorage object 值并使用 jquery 保存

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

How to Update localstorage object value & save it using jquery?如何更新 localstorage object 值并使用 jquery 保存它?

Below is the code which I have tried下面是我试过的代码

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)

I am trying to update category data from localstorage json我正在尝试从 localstorage json 更新category数据

If you remove the non necessary code(repeated code) you will have only this如果您删除不必要的代码(重复代码),您将只有这个

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

and although it doesn't make sense, it should work properly, and you need to check for typeof category !== "undefined" , maybe it doesn't exist yet in your localStorage虽然它没有意义,但它应该可以正常工作,你需要检查typeof category !== "undefined" ,也许它在你的 localStorage 中还不存在

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

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