简体   繁体   English

如何使用JSON格式的日期过滤数据

[英]How to filter the data using date from JSON format

I want to filter using date but the data is in JSON format. 我想使用日期过滤,但数据是JSON格式。 How can I filter the large dataset using date in JavaScript? 如何在JavaScript中使用日期过滤大型数据集?

Example: 例:

data=[{date:'22-12-2014',name:'selva'},{date:'10-10-2010',name:'raja'},{date:'11-11- 2011',name:'suresh'}]

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. 我确信您可以更好地完成解析日期,例如以Date构造函数接受的格式定义日期。

To grab the set of elements that match a certain date you can use filter to extract them into a new array. 要获取与特定日期匹配的元素集,您可以使用filter将它们提取到新数组中。

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. 要按日期对数据集进行排序,首先需要将日期字符串转换为JS日期对象。 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 JSFiddle演示

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

I used date format MM/DD/YY. 我使用日期格式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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Javascript 和来自 JSON 的数据在 x 轴上使用 plot 日期格式 - How to plot date format in x-axis using Javascript and data from JSON 如何使用 Javascript 根据日期值过滤 JSON 数据 - How to filter JSON data based on date value using Javascript 如何使用angularJs过滤器从JSON解析日期字段 - How to parse date field from json using angularJs filter 使用Angular按当前日期过滤JSON数据 - filter JSON data by current date using Angular 如何在javascript中按日期范围过滤json数据 - How to filter json data by date range in javascript 如何使用groupby从JSON获取数据以及如何使用lodash进行过滤 - How to get data from JSON using groupby and filter using lodash 使用新的Date()从用户输入值中减去新的Date(),从当前日期开始以MM / DD / YYYY格式比较和过滤具有日期的数据 - Compare and filter data with date in MM/DD/YYYY format from current date using using new Date() while subtracting new Date() from user input value 如何使用 jQuery 从表中以 JSON 格式发布数据 - How to post data in JSON format from table using jQuery 如何使用jQuery将JSON数组中的数据转换为表格式? - How to get data from json array into table format using jquery? 如何使用jQuery从JSON格式读取数据 - How to read data from json format using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM