Clear localstorage values with certain prefix

Using lodash. Enjoy.

_.forIn(window.localStorage, (value: string, objKey: string) => {
    if (true === _.startsWith(objKey, 'TM_')) {
        window.localStorage.removeItem(objKey);
    }
});

Removing element while iterating is unsafe, so create an array to hold the keys that need to be removed. Then, iterate over that array to remove them:

var arr = []; // Array to hold the keys
// Iterate over localStorage and insert the keys that meet the condition into arr
for (var i = 0; i < localStorage.length; i++){
    if (localStorage.key(i).substring(0,3) == 'TM_') {
        arr.push(localStorage.key(i));
    }
}

// Iterate over arr and remove the items by key
for (var i = 0; i < arr.length; i++) {
    localStorage.removeItem(arr[i]);
}