HTML5 Localstorage & jQuery: Delete localstorage keys starting with a certain word

I have 2 apps working together with localstorage and I was wondering how can I delete all the keys which start with note- and todo- . I know localstorage.clear() clears everything but thats not my aim.

Here is an example of what I have in my localstorage:

Where I want to delete all the todo-* with a button click and all note-* with other button click using jquery.

Thanks alot

Answers


Object.keys(localStorage)
      .forEach(function(key){
           if (/^todo-|^note-/.test(key)) {
               localStorage.removeItem(key);
           }
       });

I used a similar method to @Ghostoy , but I wanted to feed in a parameter, since I call this from several places in my code. I wasn't able to use my parameter name in a regular expression, so I just used substring instead.

function ClearSomeLocalStorage(startsWith) {
    var myLength = startsWith.length;

    Object.keys(localStorage) 
        .forEach(function(key){ 
            if (key.substring(0,myLength) == startsWith) {
                localStorage.removeItem(key); 
            } 
        }); 
}

Need Your Help

Are there any ways to scramble strings in python?

python string list scramble

I'm writing a program and I need to scramble the letters of strings from a list in python. For instance I have a list of strings like: