Reputation: 46222
I am currently data into a local db as following:
window.localStorage.setItem('QQIDVAL' + ID, JSON.stringify(this));
How do I go about then retrieving all of the data that is in the localstorage and looking for ones that start with "QQ"
Also how do I go about selecting which columns I want to retreive as JSON.stringify(this) has a lot of columns but I only need a subset of those.
Upvotes: 0
Views: 1843
Reputation: 17014
Many frameworks like Sencha Touch and Backbone have some basic ability simulate DB functionality using localStorage
. If you're excited to roll your own solution I would suggest doing something more like this:
// get the user store
var users = getUsers();
var john = user["john1234"];
// get all of the existing users
function getUsers() {
return JSON.parse(window.localStorage.users) || {};
}
// save users
function saveUsers(users) {
window.localStorage.users = JSON.stringify(users);
}
// add a user
function addUser(user) {
var id = user.id || new Date().getTime();
users[id] = user;
}
So my suggestion is instead of combining things based on a string separate them into separate collections or stores using standard
This is by no means a specific answer to your question but it might help you out!
Upvotes: 0
Reputation: 7877
You can store an array in your localStorage.
Something like that (i haven't managed the Jsonify. you could use Lawnchair to facilitate theses operations)
window.localStorage.setItem('QQ', {items: [] });
function addItem(id, json) {
var o = window.localStorage.getItem('QQ');
o.items[id] = json;
window.localStorage.setItem(o);
}
function getItem(id) {
return window.localStorage.getItem('QQ').items[id];
}
function getAllItems() {
return window.localStorage.getItem('QQ').items;
}
function printAllItems() {
var all = getAllItems();
for (id in all) {
console.log(id +" => "+ all[id]);
}
}
Upvotes: 1
Reputation: 53291
You can take advantage of the fact that localStorage
is treated like an object and do something like this (iterating through the keys and checking their name):
for(dataObj in localStorage) {
if(dataObj.charAt(0) == Q && dataObj.charAt(1) == Q) {
// do whatever with your data object, it starts with QQ
var myObj = localStorage[dataObj];
}
}
You could also change the if statement to this:
if(dataObj.substr(0,2) == "QQ") { // do your thing
Don't have a reference to the substr()
documentation right now though, don't recall if that's the correct syntax.
Upvotes: 0
Reputation: 19049
You should do this with databases, not storage (which is for key-value pairs). Try googling IndexedDB or Web SQL as keywords.
Upvotes: 0