最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Best way to store this data (array, object, etc) - Stack Overflow

matteradmin8PV0评论

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.

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.

Share Improve this question edited Apr 8, 2014 at 21:43 Salman Arshad 273k84 gold badges444 silver badges534 bronze badges asked Apr 8, 2014 at 9:03 rpsep2rpsep2 3,12110 gold badges41 silver badges52 bronze badges 1
  • i am pretty sure this would cause a snytax error. – Alex Commented Apr 8, 2014 at 9:09
Add a ment  | 

2 Answers 2

Reset to default 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());

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"];

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far