向已存储在 localStorage 中的对象添加属性
我在 localStorage 中有一个对象,类似于:
{"one":"oneone","two":"twotwo"}
你明白了。
由于某种原因,尝试向对象添加更多项目将不起作用。如果我想添加“三”:“三三”,它会忽略这一点,当我更新 Opera Dragonfly 中的存储区域时,它会显示相同的旧对象,未更改...
有人知道如何动态附加到对象吗?或者我是否必须在代码中附加该对象的副本,然后从 localStorage 中清除旧对象,并存储更新后的对象?我可以看到这是唯一的方法,但似乎它可能有点难看......请出主意! :D
I have an object in localStorage, something like:
{"one":"oneone","two":"twotwo"}
You get the picture.
For some reason attempting to add more items to the object won't work. If I want to add "three":"threethree", it ignores that and when I update the storage area in Opera Dragonfly it shows the same old object, unchanged...
Does anybody know how to append to objects on the fly? Or will I have to append to a copy of the object in my code, then clear the old object from localStorage, and store the updated one? I can see this being the only way, but it seems like it might be a bit ugly... Ideas please! :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为最安全、最干净的方法是从 localStorage 获取对象,解析它,添加一些数据并将其字符串化以进行存储。
就像你说的,它有点难看,但你可以创建一个函数来做到这一点
I think that the safest and cleanest way to do this is getting the object from localStorage, parsing it, add some data ans stringify it for storing.
Like you say, it is a bit ugly, but you can create a function to do this