The Storage interface has a length property telling you how many items are there, and a key method that gives you the key for item index n. So you can loop through to find the key with a for loop:
let key;
for (let n = 0, len = localStorage.length; n < len; ++n) {
const thisKey = localStorage.key(n);
if (thisKey.includes("BLABLABLA")) {
// found it
key = thisKey;
break;
}
}
if (key) {
const value localStorage.getItem(key);
// ...
}
Note that solutions using Object.keys, Object.entries, etc. won't work reliably with all storage keys — for instance, the key "length" won't work properly even though it's just fine to use "length' as a key with getItem and setItem. That's because the Storage interface already defines a property called length (the length of the storage list), so you can't access a stored item keyed by "length' using localStorage.length, you have to localStorage.getItem("length"). Object.keys and Object.entries will leave out those storage entries. (Other than it appears there's a weird bug in Chrome around the "key" key.) The above works reliably with length, key, and other similar keys (but really it's length and key that are the most problematic).
In your particular case, though, you know the key isn't length or key or any of the other things on Storage.prototype, so you could create an array of keys via Object.keys and use find to find the key:
// Read the disclaimers above
const key = Object.keys(localStorage).find(key => key.includes("BLABLABLA"));
if (key) {
const value localStorage.getItem(key);
// ...
}
...or use Object.entries to create an array of arrays as charlietfl shows. Just be mindful of the caveat.
Object.entries(localStorage)should work fine....did you try it?