Reputation: 33
i want to store this kind of information in memory all the time.
app_id admin_id url status
123 1 xyz.com 1
123 2 xyz.com 0
539 3 exmaple.com 1
876 4 new.com 1
My main concern is that i want the lookup to be easy and fast. Like at any point to query :
url
and status
for a particular admin_id
.admin_id
for a particular app_id
.SO how do i store this information in memory.
Upvotes: 2
Views: 5638
Reputation: 51030
You could do something as follows. Create an object for each row. And keep the references in and array byAdminId
for admin-id, and an object byAppId
for app-id, the add
function does that in following. getByAppId
and getByAdminId
functions for retrieval of those objects.
var store = (function() {
var byAdminId = [];
var byAppId = {};
return {
add: function(appId, adminId, url, status) {
var d = {
appId: appId,
adminId: adminId,
url: url,
status: status
};
if (appId in byAppId) { // You could also use byAppId.hasOwnProperty(appId) here
byAppId[appId].push(d);
} else {
byAppId[appId] = [];
byAppId[appId].push(d);
}
byAdminId[adminId-1] = d;
},
getByAppId: function(appId) {
if (appId in byAppId) {
return byAppId[appId];
}
return null;
},
getByAdminId: function(adminId) {
return byAdminId[adminId-1];
}
};
})();
store.add(123, 1, "abc.com", 1);
store.add(123, 9, "xyz.com", 0);
store.add(539, 3, "exmaple.com", 1);
store.add(876, 4, "new.com", 1);
var objs = store.getByAppId(123);
for (var i = 0; i < objs.length; i++) {
// access admin id as follows
// objs[i].adminId
}
var obj = store.getByAdminId(1);
// access the url and status as follows
// obj.url
// obj.status
Upvotes: 3
Reputation: 322
see this
http://dojotoolkit.org/documentation/tutorials/1.6/intro_dojo_store/
http://dojotoolkit.org/reference-guide/dojo/data.html#dojo-data
Upvotes: 0
Reputation: 992955
If you have your items in an array called a
, you could use a couple of dictionaries:
var i;
var app_id_index = {};
var admin_id_index = {};
for (i = 0; i < a.length; i++) {
app_id_index[a[i].app_id] = a[i];
admin_id_index[a[i].admin_id] = a[i];
}
Upvotes: 2