[英]how do I update a single value of an item in session storage
I have a SessionStorage
item set with the following values:我有一个具有以下值的
SessionStorage
项集:
key : state-allRequestsandResponses
value : "{"first":30,"rows":10,"sortField":"isWorkComplete","sortOrder":1}"
I want to obtain the value for first for example and change the value of 30 to say 40.例如,我想获取 first 的值并将 30 的值更改为 40。
I'm having real difficulty with using sessionStorage.setItem
to change this value.我在使用
sessionStorage.setItem
更改此值时遇到了真正的困难。 Can anybody give an example of how to change this value.谁能举例说明如何更改此值。
You can try below method in order to store, get the data and update您可以尝试以下方法来存储、获取数据和更新
let data = {"first":30,"rows":10,"sortField":"isWorkComplete","sortOrder":1}
//storing the data for the first time
sessionStorage.setItem("state-allRequestsandResponses", JSON.stringify(data));
In order to update the data in sessionStorage
get the object and parse it as json object since sessionStorage
will store the data as strings.为了更新
sessionStorage
中的数据,获取 object 并将其解析为 json object,因为sessionStorage
会将数据存储为字符串。
//Get the object and parse it
data = JSON.parse(sessionStorage.getItem("state-allRequestsandResponses"))
data.first = 40;
sessionStorage.setItem("state-allRequestsandResponses", JSON.stringify(data));
//Verify whether the data updated in sessionStorage
console.log(sessionStorage.getItem("state-allRequestsandResponses"))
Once we get the value from sessionStorage, we need convert it into JSON in order to use it as a normal object.一旦我们从 sessionStorage 中获取值,我们需要将其转换为 JSON,以便将其用作普通的 object。
try to do some changes like below..尝试做一些像下面这样的改变..
const modifiedValue = JSON.parse(sessionStorage.getItem('state-allRequestsandResponses'));
modifiedValue.first(40);
sessionStorage.setItem('state-allRequestsandResponses', JSON.stringify(modifiedValue));
Hope this helps.. :)希望这可以帮助.. :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.