How do I change a single value inside a localStorage item?

后端 未结 2 1289
不知归路
不知归路 2021-02-11 01:00

I have a set of items that get saved but I\'m trying to change a value after I retrieve it and before I pass it of to my php function. Is it possible to change just one item?

2条回答
  •  生来不讨喜
    2021-02-11 01:13

    Try this

    var args = window.localStorage.getItem("localusers");
    args["type"] = 6;
    
    //Update the localStorage with new value
    localStorage.setItem("localusers", JSON.stringify(args));
    

    Like other JavaScript objects, you can treat the localStorage object as an associative array. Instead of using the getItem() and setItem() methods, you can simply use square brackets.

    var args = localStorage["localusers"];
    args["type"] = 6;
    
    //Update the localStorage with new value
    localStorage["localusers"] = args;
    

提交回复
热议问题