Skip to content

Instantly share code, notes, and snippets.

@luizventurote
Forked from n0m4dz/findLocalItems.js
Created December 25, 2017 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizventurote/4fd9f4331ce5318d6001c993a09ffef2 to your computer and use it in GitHub Desktop.
Save luizventurote/4fd9f4331ce5318d6001c993a09ffef2 to your computer and use it in GitHub Desktop.
how to filter keys from localStorage with a regex
// returns an array of localStorage items in key/value pairs based on a query parameter
// returns all localStorage items if query isn't specified
// query can be a string or a RegExp object
function findLocalItems (query) {
var i, results = [];
for (i in localStorage) {
if (localStorage.hasOwnProperty(i)) {
if (i.match(query) || (!query && typeof i === 'string')) {
value = JSON.parse(localStorage.getItem(i));
results.push({key:i,val:value});
}
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment