3

I want to filter using date but the data is in JSON format. How can I filter the large dataset using date in JavaScript?

Example:

data=[{date:'22-12-2014',name:'selva'},{date:'10-10-2010',name:'raja'},{date:'11-11- 2011',name:'suresh'}]
3
  • What will the filter do? Commented May 14, 2014 at 9:08
  • Do you want to pull out objects by certain date, or group (sort) all of them by date? Commented May 14, 2014 at 9:16
  • 1
    BTW, that data isn't JSON (it won't parse). It's a JS object. Commented May 14, 2014 at 9:19

4 Answers 4

5

If you simply want to filter data by time, you can look through all objects in the array like this:

var filteredData = [];

for(var index in data) {
    var obj = data[index];
    var date = parseDate(obj.date);

    //Filter dates from 2011 and newer
    if(date > new Date(2011, 0, 1))
        filteredData.push(obj);
}

function parseDate(dateStr) {
    var date = dateStr.split('-');
    var day = date[0];
    var month = date[1] - 1; //January = 0
    var year = date[2];
    return new Date(year, month, day); 
}

//Filtered data now contains:
// [{"date":"22-12-2014","name":"selva"},{"date":"11-11- 2011","name":"suresh"}] 

I am sure you could do the parse date better, by for example defining the date in a format that the Date constructor accepts.

Sign up to request clarification or add additional context in comments.

1 Comment

var date = new Date(data[i]['date'].split('-').reverse()); parses that date format just ok ;)
5

To grab the set of elements that match a certain date you can use filter to extract them into a new array.

function getByDate(date){
  return data.filter(function (el) {
    return el.date == date;
  });
}

var arr = getByDate('11-11-2011');

To to sort your dataset by date you need to convert your date strings to a JS date object first. That involves adjusting the date string slightly so it can be parsed properly.

function reformatDate(date) {
  return arr = date.split('-').reverse();
}

var sortByDate = function (a, b) {
  return new Date(reformatDate(a.date)) - new Date(reformatDate(b.date));
};

data.sort(sortByDate);

JSFiddle demo

Comments

3

I used date format MM/DD/YY. Here is the full example -

var data=[
  {date:'02/10/2018',name:'date is 10'},
  {date:'02/14/2018',name:'date is 14'},
  {date:'02/16/2018',name:'date is 16'},
  {date:'02/20/2018',name:'date is 20'},
  {date:'02/24/2018',name:'date is 24'},
  {date:'02/26/2018',name:'date is 26'},
  {date:'02/30/2018',name:'date is 30'},
  {date:'03/01/2018',name:'date is 01'},
  {date:'03/05/2018',name:'date is 05'},
  {date:'02/23/2018',name:'date is name 23'},
]




  var today            = new Date();
  var todayTime        = new Date().getTime();
  var days_after_20    = new Date().setDate(today.getDate()+20);
  var days_before_5    = new Date().setDate(today.getDate()-5);

  var result = data.filter(function (item) {
     var itemTime = new Date(item.date).getTime()
     return itemTime >= days_before_5 && itemTime <= days_after_20; 
  })

console.log(result);

Comments

1

To fetch the set of elements that match a certain date you can use filter to extract them into a new array.

var tempArray=   data.filter(function (d, i) {
    return d >= startDate && d <= endDate;
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.