Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Get all keys matching a char string
Message
 
 
À
01/12/2015 04:48:04
Information générale
Forum:
HTML5
Catégorie:
Stockage local
Divers
Thread ID:
01628117
Message ID:
01628135
Vues:
40
>>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. 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. 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.
"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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform