Sometimes, we need to store data in Sessionstorage and Localstorage, and the benefits are:
1 Cached data
2 Reduce memory footprint
However, storage can only store string data, but cannot be stored directly for arrays or objects that are commonly used in JS.
var obj = {name: ' Jim '};
Sessionstorage.obj = obj;
Localstorage.obj = obj;
var arr = [1,2,3];
Sessionstorage.obj = arr;
Localstorage.obj = arr;
The above wording is not successful. But we can use the parse and stringify provided by the JSON object to convert the other data types into strings and then store them in the storage. Take a look at the code below.
var obj = {name: ' Jim '};
var str = json.stringify (obj);
deposited
sessionstorage.obj = str;
Read
str = sessionstorage.obj;
Re-convert to object
obj = Json.parse (str);
Localstorage, too, is just different from sessionstorage storage time.
It should be noted that the array in JS is also an object type, so the above code is also applicable to the arrays.