gpt4 book ai didi

javascript - JS 数据过滤器和组不正确

转载 作者:行者123 更新时间:2023-11-30 11:04:15 25 4
gpt4 key购买 nike

我使用字典数组,我想按时间段对数据进行分组,我有一个解决方案。

但现在我发现了问题,最后几个小时的数据没有出现在分组数组中。

代码:

    // 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" },
{ item: "item", category: "category", dateTime: "19/05/23 14:27:33" },
{ item: "item", category: "category", dateTime: "19/05/23 14: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", "14:00 - 14:45"];

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

正如您在数组中看到的那样,过滤数据未正确显示后,我有时间 14:00 - 14:45 的数据。

在我的数组中有两条记录:

{ item: "item", category: "category", dateTime: "19/05/23 14:27:33" },
{ item: "item", category: "category", dateTime: "19/05/23 14:37:33" },

我试图找到解决方案,但我卡住了。

预期输出:

{
"time": "14:00 - 14:45",
"groupedData": [{ item: "item", category: "category", dateTime: "19/05/23 14:27:33" },
{ item: "item", category: "category", dateTime: "19/05/23 14:37:33" }]

}

最佳答案

可以直接比较时间,在结果集中先找组再找项。

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" }, { item: "item", category: "category", dateTime: "19/05/23 14:27:33" }, { item: "item", category: "category", dateTime: "19/05/23 14:37:33" }],
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", "14:00 - 14:45"],
result = array.reduce((r, o) => {
var part = o.dateTime.slice(9, 14),
group = time.find(t => {
var [min, max] = t.split(' - ');
return min <= part && part < max;
}),
temp = r.find(({ time }) => time === group);

if (!group) return r;
if (!temp) {
r.push({ time: group, groupedData: [o] });
return r;
}
temp.groupedData.push(o);
return r;
}, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - JS 数据过滤器和组不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com