Dhanesh Mane Dhanesh Mane - 2 months ago 6
JSON Question

which of the following JSON is better to work with specially when we have to select data between 2 dates?

Which of the following JSON format will be better. According to me second will be better as I can directly find the data of particular date just by array index. which should I prefer.

First

[{
"date" : "20100808", //2010-08-08
"data" : "one"
},
{
"date" : "20100809",
"data" : "Two"
}]


Second

[{
"20100808" : "one"
},
{
"20100809" : "Two"
}]

Answer

When you want to select data between two dates, the first one is better. You can write something like

data = [{
  "date" : "20100808", //2010-08-08
  "data" : "one"
 },
 {
  "date" : "20100809",
  "data" : "Two"
}];

dataBetwnTwoDates = data.filter(function(d) {
  var date = new Date(d.date);               // insert your function to convert "20100808" into Date object and store in date.
  return +date > +date1 && +date < date2;    // +date will convert Date object to integer; date1 and date2 are Date objects in between whom you want the data to be.
});