Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to store this data (array, object, etc)

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.

like image 339
rpsep2 Avatar asked Apr 08 '14 09:04

rpsep2


2 Answers

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());
like image 80
Salman A Avatar answered Sep 29 '22 02:09

Salman A


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"];
like image 45
Rajaprabhu Aravindasamy Avatar answered Sep 29 '22 01:09

Rajaprabhu Aravindasamy