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

标签 javascript

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

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

代码:

    // 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/

相关文章:

javascript - Cordova 检索并删除超过 30 天的文件?

javascript - 元素边框上的html鼠标悬停事件

javascript - 跨浏览器 JS/jQuery 将当前 URL 复制到剪贴板

javascript - 关于向服务器提交正确数据的 javascript 的安全性

javascript - meteor JS : How to stub validated method in unit test

javascript - 确认 Javascript 中的字符串中只包含数字

javascript - 为什么 Google 优化 javascript 不在页面刷新时执行?

javascript - Handlebars - 通过嵌套的 each 中的外部键访问数组

javascript - 单击文本框事件冒泡

javascript - 为什么修改一个实例上的值会改变另一个实例上的值?