Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get all keys matching a char string
Message
 
 
General information
Forum:
HTML5
Category:
Local storage
Miscellaneous
Thread ID:
01628117
Message ID:
01628138
Views:
29
>>>>Hi,
>>>>
>>>>Say I have the following strings (serialized JSON) (Key and Value in the local storage (simplified):
>>>>
>>>>
>>>>"PM_ABCD","long string is here"
>>>>"PM_1233", "another long string here"
>>>>"ABC", "Another long string"
>>>>"PM_G134555","long string"
>>>>
>>>>
>>>>Can I get all keys - into an object - (without getting the Value) that start with a string "PM_"?
>>>
>>>As Rob says you will need to iterate localStorage:
String.prototype.startsWith = function(startText) {
>>>        return this.slice(0, startText.length) == startText;
>>>    };
>>>    
>>>    var pmKeys = new Array();
>>>    for (var x = 1; x < localStorage.length; x++) {
>>>        var s = localStorage.key(x);
>>>        if (s.startsWith("PM_")) {
>>>            pmKeys.push(s);
>>>        }
>>>    }
But it may be better to store all those items as an array of objects under one key.e.g:
var toStore = [
>>>    { Key: "PM_ABCD", Value: "long string is here" },
>>>    { Key: "PM_1233", Value: "another long string here" },
>>>    { Key: "ABC", Value: "Another long string here" },
>>>    { Key: "PM_G134555", Value: "long string" }
>>>];
>>>localStorage.setItem("MyList", JSON.stringify(toStore));
Then you can pull them back into an array which is easier to manipulate:
var myList = JSON.parse(localStorage.getItem("MyList"));
>>>var pmKeys = new Array();
>>>    myList.forEach(function(item) {
>>>        if (item.Key.slice(0, 3) == "PM_") {
>>>            pmKeys.push(item.Key);
>>>        }
>>>    });
I usually pull any stuff that I'm likely to need from localStorage into memory when the page loads.
>>
>>Thank you very much for the code and the suggestion. I will have to try the approach of array of objects. But I should be able to delete or replace any one of the "items". For example, I should be able to remove the item "PM_1233" and the value or replace the Value of this key. So I will have to learn how to do it.
>
>I just get the whole array into memory, modify/add/delete as required then write the whole thing back to localStorage. Don't seem to have any speed concerns...
>
> >Another concern I have is if there is enough capacity to store all the "data" I need to store in the localStorage. I read that local storage is limited to 5MB. I >would have to run some tests to see if this is sufficient for the customer storage.
>
>Quota depends on the browser (and on some the user can adjust the value). Don't know if this is up to date:http://www.sitepoint.com/html5-local-storage-revisited/
>
>>I want to watch (later) a pluralsight course on IndexedDB. I know nothing about it now but would like to know if this is an alternative to a local storage with >higher capacity.
>
>Don't know much about it either - but I think there are a couple of libraries which make for a more developer-friendly approach. This looks interesting:http://mozilla.github.io/localForage/#localforage.
>
>Seems it will use localStorage as a fall-back if IndexDB is not available....

Since my target audience is iPad Mini (which means Safari) I quickly googled for IndexedDB and Safari on iOS and found claims that IndexedDB on iOS8 and 8.1 are buggy. This is bad. So I need to create a real case of customer data (which is not so easy) to see if localStorage is enough. Another approach is to tell the customer that storing locally is not an option and store on the server. Actually I am pretty much convinced that I won't be able to deliver the program that will work in a completely disconnected fashion (that it, with no WiFi).
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform