How to filter the data using date from JSON format

你说的曾经没有我的故事 提交于 2021-02-18 17:15:27

问题


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'}]

回答1:


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.




回答2:


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




回答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);



回答4:


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;
})


来源:https://stackoverflow.com/questions/23650065/how-to-filter-the-data-using-date-from-json-format

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!