我使用dictionarry数组,在这里存储时间戳。
所以我有一个表,需要按时间存储数据。
我想按时间戳和日期过滤数据。
// Dictionary array:
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" }
];
// And I have time periods, for ex:
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",];
//I try to use this method:
const groups = array.reduce((groups, item) => {
const date = item.dateTime.split('T')[0];
if (!groups[date]) {
groups[date] = [];
}
groups[date].push(item);
return groups;
}, {});
const groupArrays = Object.keys(groups).map((date) => {
return {
date,
games: groups[date]
};
});
console.log(groupArrays);
所以,最后,我想按小时接收分组日期。
最后我想收到:
var result = [
{ time: "05:45 - 06:00", groupedData:[
{ item: "item", category: "category", dateTime: "19/05/23 05:46:33" },
{ item: "item", category: "category", dateTime: "19/05/23 05:21:33" }
]
time: "06:00 - 07:00", groupedData:[
{ item: "item", category: "category", dateTime: "19/05/23 06:31:33" },
{ item: "item", category: "category", dateTime: "19/05/23 06:46:33" }
]
];
我真的很困,我已经厌倦了寻找解决方案。
答案 0 :(得分:1)
您需要处理日期格式以便比较日期,我的解决方案仅包含小时,您可能需要在过滤器函数中包含分钟比较,以便获得准确的结果。
说明:
希望这会有所帮助。
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 = new Date(validDate).getHours();
let result = false;
if(date >= min && date < max) {
return true;
} else {
return false;
}
});
let date = currentValue[0] + " - " + currentValue[1];
groups.push({
time: date,
groupedData: filtered
});
return groups;
}, []);
console.log(grouped);
答案 1 :(得分:1)
尝试一下,这可以满足您的要求。但是它将为“数组”中的每个对象添加一个额外的属性“ seconds”。如果需要,可以在最后删除它。
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" }
];
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) {
var object = {};
object["time"] = time[k];
object["groupedData"] = array[l];
result.push(object);
}
}
}
function getSeconds(date) {
//date - "19/05/23 06:31:33";
time = date.split(" ")[1].split(":");
return (+time[0]) * 60 * 60 + (+time[1]) * 60 + (+time[2]);
}