我正在尝试更新localstorage中的属性。我不想循环并重置整个事情。这可能吗?
var aR = [
{ "id": 1, "width": 500 },
{ "id": 2, "width": 400 }
]
localStorage.setItem('test', JSON.stringify(aR));
// need to update property
localStorage['test'][1]['width'] = '720';
答案 0 :(得分:4)
您可以执行stringify,JSON.parse()
var arrayString = localStorage.getItem('test');
var a = JSON.parse(arrayString);
a[1]['width'] = '720';
// and do the same thing, stringify and store it back to local storage
希望这会有所帮助:)