JS数据过滤器和组不正确

时间:2019-05-27 07:32:02

标签: 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" }]

 }

3 个答案:

答案 0 :(得分:2)

您可以直接比较时间,然后先找到组,然后再找到结果集中的项目。

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

答案 1 :(得分:1)

在您的代码中,您仅比较小时数而忽略分钟。这会导致您在14:00 - 14:45期间遇到问题。

由于日期的格式设置以及执行比较的方式,您可以在此处进行基于字符串的比较:

const 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" },
    ];

const times = ["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"];

const grouped = times.map(time => {
  const [min, max] = time.split(' - ');
  const groupedData = array.filter(({dateTime}) => {
    const ts = dateTime.substring(9, 14);
    
    return ts >= min && ts < max;
  });
  
  return {time, groupedData};
});

console.log(grouped);

答案 2 :(得分:0)

    const 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:
    const times = ["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"
    ];

    const result = [];

    times.forEach((time) => {

        const [min, max] = time.split(" - ");

        const groupedData = array.filter(({ dateTime }) => {
            return `${min}:00`.localeCompare(dateTime.slice(-8)) <= 0 && `${max}:00`.localeCompare(dateTime.slice(-8)) >= 0;
        })

        result.push({
            time,
            groupedData
        });
    });


    console.log('@>result', result)