Localstorage: counts the number of values ​​in a key when using stringify

How can I calculate how many different values ​​I have in 1 local file.

For instance:

This is my local store:

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

In this example, I would like to receive a warning about two different values. So basically I want to count {} ..

Is it possible?

+4
source share
3 answers

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

However, I think I might misunderstand you. You mean that you have one key, and this key value is the JSON.stringify 'd object? If so, you can do JSON.parse(localStorage.keyname).length to non-sterilize the value and count the number of people there.

+7
source

Your object is saved as an array, so just using .length, you must specify the number of {} pairs that you have. Try this (replace "independently" with the name of your array:

 var something = localstorage.whatever[]; var length = something.length; alert("length = " + length); 
+2
source

This will return the number of items in your key.

 testing = JSON.parse(localStorage.getItem(KEYNAMEHERE)); //Lets count how many we have back obj = eval('(' + testing + ')'); //And the total is... objCount=0; for(_obj in obj) objCount++; alert(objCount) 
0
source

Source: https://habr.com/ru/post/1380754/


All Articles