Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localstorage update property in array

I am trying to update a property in localstorage. I don't want to loop through and reset the whole thing. Is this possible?

    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';
like image 489
Killilea Avatar asked Nov 30 '25 02:11

Killilea


1 Answers

You can do the opposite of 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

Hope this helps :)

like image 133
Arkantos Avatar answered Dec 02 '25 16:12

Arkantos