I have created an angular native script project and i am using the "nativescript-localstorage". I want to get all the keys in the storage ,how will i do it
Asked
Active
Viewed 266 times
0
-
Possible duplicate of [How can I show all the localStorage saved variables?](https://stackoverflow.com/questions/5410820/how-can-i-show-all-the-localstorage-saved-variables) – msanford Oct 15 '18 at 13:59
4 Answers
2
You can loop through all the localStorage items and use the localStorage.key(index) to get the key and use it in localStorage.getItem to get the value.
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i);
let val = localStorage.getItem(key);
}
William Juan
- 1,247
- 1
- 7
- 10
0
You can use localStorage
Creating Entries
let key = 'Item 1';
localStorage.setItem(key, 'Value');
Reading Entries
let myItem = localStorage.getItem(key);
Updating Entries
localStorage.setItem(key, 'New Value');
Deleting Entries
localStorage.removeItem(key);
Clearing Everything
localStorage.clear();
Sunil Singh
- 10,307
- 2
- 23
- 45
0
You need to use localstorage.length that will return you the number of keys stored.
console.log("Keys stored", localStorage.length);
You can also iterate for all the keys stored in localstorage localstorage.key return the key name at this position
for (let i = 0; i < localStorage.length; i++) {
let key = localStorage.key(i); // Will return the name of the key
let val = localStorage.getItem(key); // will return the value
}
Narendra
- 4,434
- 2
- 18
- 35
0
get length of local storage keys. returns number of keys stored
localStorage.length
Return the key name at this position
localStorage.key(id)
let n = localStorage.length;
for(let i=0;i<n;i++){
console.log("--->",localStorage.key(i));
}
D C
- 628
- 1
- 6
- 15