5

I need to store (many) objects or arrays of data, which need to have the following criteria:

I need to be able to add a new set of data into the existing data easily

I need to be able to sort the data by date/ time added easily (array in order of when entries were pushed to it)

I need to be able to grab an entry easily using a reference, either integer or string. This is important, at the moment I have to do an $.each() to loop through my data until I find the entry I want.

I have tried using a structure like:

saved_info = {
    1001: {//all my data for ref 1001},
    1002: {//all my data for ref 1002}
}

which gave me what wanted of being able to grab the info easily given a reference:

info = saved_info[1001];

however, the reference numbers I use aren't in order - I use a reference given to me (its a unique identifier), therefore the object isn't in order of when items were added/saved/pushed.

Salman A
  • 262,204
  • 82
  • 430
  • 521
rpsep2
  • 3,061
  • 10
  • 39
  • 52

2 Answers2

3

You can use two objects:

  • One that stores the data by key
  • Another that stores the sort order

This way you can (i) lookup an element by key (ii) loop over elements in the order they were inserted. Rough outline of the structure:

var DataObject = {
    data: {},
    sort: []
};

Here is how you add data to this structure:

DataObject.data[1004] = {name: "Test 4"};
DataObject.sort.push(1004);

DataObject.data[1001] = {name: "Test 1"};
DataObject.sort.push(1001);

DataObject.data[1003] = {name: "Test 3"};
DataObject.sort.push(1003);

DataObject.data[1002] = {name: "Test 2"};
DataObject.sort.push(1002);

Here is how you perform a random access:

console.log(DataObject.data[1001].name);
console.log(DataObject.data[1003].name);

And here is how you iterate over all elements in the order they were added:

var i;
for (i = 0; i < DataObject.sort.length; i++) {
    console.log(DataObject.data[DataObject.sort[i]].name);
}

It is possible to wrap the entire logic inside a class:

function DataObject() {
    this.data = {};
    this.sort = [];
    this.setItem = function (k, v) {
        this.data[k] = v;
        this.sort.push(k);
   };
    this.getItemByKey = function (k) {
        return this.data[k];
   };
    this.getItemByPos = function (i) {
        return this.data[this.sort[i]];
   };
    this.getAllItems = function () {
        var i, r = [];
        for (i = 0; i < this.sort.length; i++) {
            r.push(this.data[this.sort[i]]);
       }
        return r;
   };
}

var t = new DataObject();

t.setItem(1001, {name: "Test 1"});
t.setItem(1002, {name: "Test 2"});
t.setItem(1003, {name: "Test 3"});
t.setItem(1004, {name: "Test 4"});

console.log(t.getItemByKey(1001));
console.log(t.getItemByPos(0));
console.log(t.getAllItems());
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • thank you this is amazing - I have another question though. Once I have all my info in a variable, i would like to store it in localStorage. at the moment, when I retrieve the info back out of saved storage and run for e.g .getAllItems on it, i get 'Object [object Object] has no method 'getAllItems'. is there a way to get around this? – rpsep2 Apr 08 '14 at 11:35
  • See: http://stackoverflow.com/q/2010892/87015 Seems like you cannot store objects in localStorage directly. You can add utility methods to the class such as `saveToLocalStorage` and `restoreFromLocalStorage` that store/retrieve `data` and `sort` as a JSON encoded string. – Salman A Apr 08 '14 at 11:45
2

Try to build a Json like this,

var xJson = {
    "1001":{//all my data for ref 1001},
    "1002":{//all my data for ref 1002}
};

and you can fetch the records as per your wish using the bracket notation, since we are using a numeric value as a key.

var xData = xJson["1001"];
Krzysztof Romanowski
  • 2,164
  • 2
  • 20
  • 29
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130