store object in the SessionStorage [ 763 views ]
Goal: to store object on the client side
The following code is simple not working:
var obj = {id: 1, name: 'cat'}; sessionStorage["pet"] = obj; ... console.log(sessionStorage["pet"]);
the result is: [object Object]
We need to stringify the object before the store like this:
var obj = {id: 1, name: 'cat'}; sessionStorage["pet"] = JSON.stringify(obj); ... console.log(JSON.parse(sessionStorage["pet"]));
now the result is: Object {id: 1, name: 'cat'}
and we are happy…