Javascript localStorage 对象处理数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13702100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:41:53  来源:igfitidea点击:

localStorage array of objects handling

javascriptarrayshtmlobjectlocal-storage

提问by sergionni

Array of JSON objects are stored in HTML5 localStorage.
For now delimiter is ;
For accessing and modifying array of objects from localStorage, split(';')and join(';')operations used.

JSON 对象数组存储在 HTML5 中localStorage
现在分隔符 用于访问和修改对象数组, 以及使用的操作。;
localStoragesplit(';')join(';')

However ,delimiter approach looks unstable.
For instance ;could be met inside objects attribute and split(';')operation will be uncorrect.

但是,分隔符方法看起来不稳定。
例如;可能在对象属性内遇到并且split(';')操作将不正确。

It could be used ;;for delimiter,but i'm not certain it will be stable also.

它可以用于;;分隔符,但我不确定它是否也会稳定。

Is there any robust way to handle localStoragepresented as array of objects,as far localStoragesaved as String?

有没有任何健壮的方法来处理localStorage呈现为对象数组,就localStorage保存而言String

EDIT

编辑

one of stoppers is that array of object couldn't be saved to localStorage as classical: "[{},{}]"
localStorageconverts it automatially to String like "{},{}"

阻碍之一是对象数组不能像经典一样保存到 localStorage:"[{},{}]"
localStorage将其自动转换为 String like"{},{}"

my current data within localStorage:

我目前的数据在localStorage

"{"name":"volvo","id":"033"};{"name":"saab","id":"034"}"

assumption
perhaps,i can add [at the start and ]at the end,but it looks not gracefull

假设
也许,我可以[在开头和]结尾添加,但它看起来不优雅

回答by Cerbrus

Just convert the objects to JSON strings:

只需将对象转换为 JSON 字符串:

localStorage.setItem("savedData", JSON.stringify(objects));

And vice versa:

反之亦然:

objects = JSON.parse(localStorage.getItem("savedData")));

Or you can add multiple objects in the same localStorage value:

或者您可以在同一个 localStorage 值中添加多个对象:

localStorage.setItem("savedData", JSON.stringify([object1, object2 /*, etc*/]));
object1 = JSON.parse(localStorage.getItem("savedData"))[0];
object2 = JSON.parse(localStorage.getItem("savedData"))[1];

Here's the DOM storage specification.

这是 DOM 存储规范

You can also access savedDatalike this:

您也可以savedData这样访问:

localStorage.savedData = "Hello world"
var foo = localStorage.savedData;

This can be used for both getting and setting the data, but it is considered less "safe" than getItem('name');and setItem('name', 'value');

这可用于获取和设置数据,但被认为不如getItem('name');和更“安全”setItem('name', 'value');

回答by RRikesh

Read variables:

读取变量:

var xyz = JSON.parse( localStorage.getItem( 'element' ) );

var xyz = JSON.parse( localStorage.getItem( 'element' ) );

Store variables:

存储变量:

localStorage.setItem( 'element' , JSON.stringify(xyz));

localStorage.setItem( 'element' , JSON.stringify(xyz));

where elementis the name of the local storage variable and xyzthe name of the js variable.

其中element是本地存储变量xyz的名称和js变量的名称。

回答by Adrian May

You can use parse and stringify if the array isn't too big, but if it is, then you'll be re-encoding the whole data set for every little change.

如果数组不是太大,您可以使用 parse 和 stringify,但如果是,那么您将针对每个小的更改重新编码整个数据集。

The library at http://rhaboo.orgimproves on that by giving each array entry its own localStorage entry in a linked list. That means you can make changes quite efficiently. Unlike JSON, it also honours text-named properties and three different types of sparse entry (null, undefined and simply not there.)

http://rhaboo.org上的库通过在链接列表中为每个数组条目提供其自己的 localStorage 条目对此进行了改进。这意味着您可以非常有效地进行更改。与 JSON 不同,它还支持文本命名的属性和三种不同类型的稀疏条目(空、未定义和根本不存在。)

BTW, I wrote rhaboo.

顺便说一句,我写了 rhaboo。