Localstorage:计算使用stringify时键中有多少个值 [英] Localstorage: Count how many values in key when using stringify
问题描述
如何计算1个本地存储密钥中有多少个不同的值.
How can I count how many different values I have in 1 localstorage key.
例如:
这是我的本地存储空间:
This is my localstorage:
[{"id":"item-1","icon":"google.com"},{"id":"item-2","icon":"youtube.com"}]
在此示例中,我想要2个不同值的警报.所以基本上我想计算{}
..
For this example I'd like an alert of 2 different values. So basically I want to count the {}
..
有可能吗?
推荐答案
localStorage
是一个对象,因此仅localStorage.length
即可提供条目数.
localStorage
is an object, so just localStorage.length
gives you the number of entries.
但是我想我可能会误会你.您是说您拥有一个键,而该键的值是一个JSON.stringify
对象?如果是这样,您可以执行JSON.parse(localStorage.keyname).length
来反序列化值并计算其中的数量.
However I think I might be misunderstanding you. Do you mean you have one key, and that key's value is a JSON.stringify
'd object? If so, you could do JSON.parse(localStorage.keyname).length
to unserialise the value and count how many there are in there.
这篇关于Localstorage:计算使用stringify时键中有多少个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!