admin管理员组文章数量:1319023
I'm trying to store and update an array in the localstorage using JSON.parse/stringify. But it doesn't seem to be working.
yesArray = JSON.parse(localStorage.getItem(yesArray));
yesArray.push("yes");
localStorage.setItem("yesArray", JSON.stringify(yesArray));
Am I all wrong with this?
I'm trying to store and update an array in the localstorage using JSON.parse/stringify. But it doesn't seem to be working.
yesArray = JSON.parse(localStorage.getItem(yesArray));
yesArray.push("yes");
localStorage.setItem("yesArray", JSON.stringify(yesArray));
Am I all wrong with this?
Share Improve this question asked Dec 6, 2016 at 0:04 SausejiiSausejii 631 gold badge1 silver badge5 bronze badges 2- What error do you see in console? – Ajay Narain Mathur Commented Dec 6, 2016 at 0:07
-
5
getItem(yesArray)
->getItem("yesArray")
typo? – Alexander O'Mara Commented Dec 6, 2016 at 0:08
2 Answers
Reset to default 3This seems to be the problem with passing the key of local storage without quotes.
While reading from local storage use the key as argument as it stores the value as key/value pairs.
yesArray = JSON.parse(localStorage.getItem("yesArray"));
Missing quotes around yesArray
in the first line?
yesArray = JSON.parse(localStorage.getItem('yesArray'));
Sample:
var yesArray = [];
localStorage.setItem('yesArray', JSON.stringify(yesArray));
yesArray = JSON.parse(localStorage.getItem('yesArray'));
yesArray.push('yes');
localStorage.setItem('yesArray', JSON.stringify(yesArray));
JSON.parse(localStorage.getItem('yesArray')); // Returns ["yes"]
本文标签: Updating localstorage arrays in JavascriptStack Overflow
版权声明:本文标题:Updating localstorage arrays in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742051408a2418081.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论