数组中的localstorage update属性

时间:2015-02-11 17:41:43

标签: javascript jquery local-storage

我正在尝试更新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';

1 个答案:

答案 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

希望这会有所帮助:)