0

NOTE: Language i am using is Javascript

I have a an array of objects. Each object has three properties: year, date, title. For example:

[ 
  {
    year: 2013, date: "23/10/2013", title: "Title1"
  }, 
  {
    year: 2012, date: "4/2/2012", title: "Title2"
  }
]

I need to make an efficient data structure from this array such that:

  • All objects with same year are grouped together, and groups are sorted on the basis of "year"
  • All objects with same date and title are grouped together. Objects with different dates are sorted.

The data structure should be efficient for reading and traversing (i need to present them in some sort of timeline).

G.D. Singh
  • 173
  • 1
  • 18

1 Answers1

3

So, you probably want something like this:

var objects = {
    "2012":{
        "4/2/2012":{
            "title1":[
                //array of objects
            ],
            "title2":[
                //array of objects
            ],
            // etc
        },
        "5/9/2012":[
            "title3":[/*objects*/],
        ],
    },
    "2013":{
        // etc
    }
}

Then you can just access the array of objects them like this:

objects["2012"]["5/9/2012"]["title1"]

So:

objects["year"]["date"]["title"];
Cerbrus
  • 70,800
  • 18
  • 132
  • 147