Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localstorage: Count how many values in key when using stringify

How can I count how many different values I have in 1 localstorage key.

For example:

This is my localstorage:

[{"id":"item-1","icon":"google.com"},{"id":"item-2","icon":"youtube.com"}]

For this example I'd like an alert of 2 different values. So basically I want to count the {}..

Is that possible?

like image 804
jQuerybeast Avatar asked Dec 16 '22 07:12

jQuerybeast


1 Answers

localStorage is an object, so just localStorage.length gives you the number of entries.

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.

like image 73
Niet the Dark Absol Avatar answered Jan 07 '23 10:01

Niet the Dark Absol