简体   繁体   中英

Update localstorage object value & save it using jquery

How to Update localstorage object value & save it using 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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