Jquery How To Filter Two Dates And Time From Dictionary, And Group At New Array
I work with a dictionary array, and here I store a time stamp. So I have a table for which I need to store data by time. I want to filter data by time stamp and date. But it's n
Solution 1:
You need to work around the date format in order to compara dates, my solution includes only the hours, you might need to include the minutes comparison inside the filter function in order to obtain accurate results.
Explanation:
- First I formatted the date in order to read it and compare.
- Use the reduce function to obtain the grouped data.
- Include a filter function so we can obtain items that match the current value inside the reduce function and push the items inside the result.
- Output the result
Hope this helps.
var array = [
{ item: "item", category: "category", dateTime: "19/05/23 05:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 05:21:33" },
{ item: "item", category: "category", dateTime: "19/05/23 06:31:33" },
{ item: "item", category: "category", dateTime: "19/05/23 06:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 07:34:33" },
{ item: "item", category: "category", dateTime: "19/05/23 07:55:33" },
{ item: "item", category: "category", dateTime: "19/05/23 08:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 09:16:33" },
{ item: "item", category: "category", dateTime: "19/05/23 09:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 10:36:33" },
{ item: "item", category: "category", dateTime: "19/05/23 11:47:33" },
{ item: "item", category: "category", dateTime: "19/05/23 11:55:33" },
{ item: "item", category: "category", dateTime: "19/05/23 12:37:33" }
];
var time = ["05:45 - 06:00", "06:00 - 07:00", "07:00 - 08:00", "08:00 - 09:00", "09:00 - 10:00", "10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00", "13:00 - 14:00",];
var formatedTime = time.map(function(val) {
return val.split(" - ");
});
var grouped = formatedTime.reduce((groups, currentValue, cI) => {
let min = parseInt(currentValue[0].substring(0,2));
let max = parseInt(currentValue[1].substring(0,2));
let filtered = array.filter(val => {
let validDate = val.dateTime.replace(/\//g,'-').replace(' ','T').replace('19','2019');
let date = newDate(validDate).getHours();
let result = false;
if(date >= min && date < max) {
returntrue;
} else {
returnfalse;
}
});
let date = currentValue[0] + " - " + currentValue[1];
groups.push({
time: date,
groupedData: filtered
});
return groups;
}, []);
console.log(grouped);
Solution 2:
Try this, This covers your requirment. But It will add an extra property "seconds" to each object in your "array". You could remove this at the end if you would like to.
vararray = [
{ item: "item", category: "category", dateTime: "19/05/23 05:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 05:21:33" },
{ item: "item", category: "category", dateTime: "19/05/23 06:31:33" },
{ item: "item", category: "category", dateTime: "19/05/23 06:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 07:34:33" },
{ item: "item", category: "category", dateTime: "19/05/23 07:55:33" },
{ item: "item", category: "category", dateTime: "19/05/23 08:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 09:16:33" },
{ item: "item", category: "category", dateTime: "19/05/23 09:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 10:36:33" },
{ item: "item", category: "category", dateTime: "19/05/23 11:47:33" },
{ item: "item", category: "category", dateTime: "19/05/23 11:55:33" },
{ item: "item", category: "category", dateTime: "19/05/23 12:37:33" }
];
array.forEach(obj => obj["seconds"] = getSeconds(obj.dateTime));
var time = ["05:45 - 06:00", "06:00 - 07:00", "07:00 - 08:00", "08:00 - 09:00", "09:00 - 10:00", "10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00", "13:00 - 14:00"];
var result = [];
for (var k=0; k<time.length; k++){
var range = time[k]; //"05:45 - 06:00";
range = range.split("-");
var hrs_mins = range.map(x => x.trim());
var lowerLimit = hrs_mins[0].split(":");
lowerLimit = lowerLimit[0] * (60 * 60) + lowerLimit[1] * 60;
console.log(lowerLimit);
var upperLimit = hrs_mins[1].split(":");
upperLimit = upperLimit[0] * (60 * 60) + upperLimit[1] * 60;
console.log(upperLimit);
for (var l=0; l< array.length; l++) {
if( array[l].seconds >= lowerLimit && array[l].seconds < upperLimit) {
varobject = {};
object["time"] = time[k];
object["groupedData"] = array[l];
result.push(object);
}
}
}
functiongetSeconds(date) {
//date - "19/05/23 06:31:33";
time = date.split(" ")[1].split(":");
return (+time[0]) * 60 * 60 + (+time[1]) * 60 + (+time[2]);
}
Post a Comment for "Jquery How To Filter Two Dates And Time From Dictionary, And Group At New Array"