How to Change Specific Values in KVS (Session Storage)

Asked 1 years ago, Updated 1 years ago, 45 views

I'm a beginner at KVS. I am currently fiddling with sessionStorage

For example,

 {id01:{hoge:1, moge:2, high:5}}

and other records.

I would like to change only the moge value to 4, but for now I can only think of getting all the id01, rewriting the moge, and re-save the whole id01.

id01.moge=4

Can't you do that?

javascript html5

2022-09-30 16:42

1 Answers

I don't know how sessionStorage handles hash, but basically sessionStorage is the key and value type.

Each Storage object offers access to a list of key/value pairs, which are some items called items. Keys are strings.Any string(include the empty string) is a valid key.Values are similar strings

http://dev.w3.org/html5/webstorage/ #the-storage-interface

Therefore, when saving hash in value, it is also saved as String (for example, JSON.stringify()), and when retrieving the value, it is combined (for example, JSON.parse().

Therefore, it is not possible to treat value directly in sessionStorage as an object, so you may need to update it and save it again (as shown below).

//Save
vardata={id01:{hoge:1, moge:2, high:5}};
sessionStorage.setItem('id01', JSON.stringify(data)));

// acquisition
var str = sessionStorage.getItem('id01');
variable = JSON.parse(str);
// update
item.moge=4
sessionStorage.setItem('id01', JSON.stringify(item));


2022-09-30 16:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.