-
-
Save luizventurote/4fd9f4331ce5318d6001c993a09ffef2 to your computer and use it in GitHub Desktop.
how to filter keys from localStorage with a regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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