简体   繁体   中英

How to check if an item exists in local storage without having the full path available?

I need to check wether a file exists in localStorage.

The filepath consists of

id.version

and I only have a the id value to test, which will be a 10 character string followed by the version which is of random length.

I'm used to checking for file existence server side using Coldfusion, where I can do something like this:

fileExists(expandPath(../some/path/to/a/file.*) )

where * would be any character.

Question :
Is something like this possible using regex, so I could test whether a file that contains the id parameter and any number of characters following the id exist? I don't need to retrieve the file itself, just know if there is at least one with matching id in the path.

Thanks for help!

You can iterate over each localstorage item and get the key with the .key() method and check if that contains the id string etc. or use a regex to do something similar, Figuring out how to check the value and not the key, if that's where the ID resides, should be trivial :

for (var i = 0; i < localStorage.length; i++){
    if ( localStorage.key(i).indexOf(id) != -1 ) {
        var item = localStorage.getItem(localStorage.key(i));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM