localStorage - append an object to an array of objects

懵懂的女人 提交于 2019-12-03 16:45:45

You can only store strings in localStorage. You need to JSON-encode the object then store the resulting string in localStorage. When your application starts up, JSON-decode the value you saved in localStorage.

localObj = {}
localObj.localArray = []
localObj.localArray.push(arrayObj)

localStorage.localObj = JSON.stringify(localObj);
...
localObj = JSON.parse(localStorage.localObj);

LocalStorage can store only key-value pairs where key is string and value is string too. You can serialize your whole object hierarchy into JSON and save that as localStorage item.

According to V8 sources (I assume you using Google Chrome but hope this isn't different for other JS engines) TypeError with message 'non_object_property_call' occurs than method called on undefined or null value.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!