JS数据过滤器和组不正确

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


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


代码:


    // 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" },

    ];



因此,正如您在数组中看到的那样,我在过滤数据后的时间为 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" }]


 }


互换的青春
浏览 142回答 3
3回答

慕运维8079593

您可以直接比较时间并在结果集中先找到组,然后找到项目。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" }],&nbsp; &nbsp; 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"],&nbsp; &nbsp; result = array.reduce((r, o) => {&nbsp; &nbsp; &nbsp; &nbsp; var part = o.dateTime.slice(9, 14),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group = time.find(t => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var [min, max] = t.split(' - ');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return min <= part && part < max;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = r.find(({ time }) => time === group);&nbsp; &nbsp; &nbsp; &nbsp; if (!group) return r;&nbsp; &nbsp; &nbsp; &nbsp; if (!temp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r.push({ time: group, groupedData: [o] });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; temp.groupedData.push(o);&nbsp; &nbsp; &nbsp; &nbsp; return r;&nbsp; &nbsp; }, []);console.log(result);.as-console-wrapper { max-height: 100% !important; top: 0; }

湖上湖

在您的代码中,您只比较小时数而忽略分钟数。这会导致您在14:00 - 14:45期间遇到问题。由于日期的格式以及执行比较的方式,您可以在此处进行基于字符串的比较:const array = [&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 05:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 05:21:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 06:31:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 06:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 07:34:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 07:55:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 08:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 09:16:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 09:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 10:36:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 11:47:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 11:55:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 12:37:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 14:27:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 14:37:33" },&nbsp; &nbsp; ];const times = ["05:45 - 06:00", "06:00 - 07:00",&nbsp; &nbsp; &nbsp;"07:00 - 08:00", "08:00 - 09:00", "09:00 - 10:00",&nbsp; &nbsp; &nbsp;"10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00",&nbsp; &nbsp; &nbsp;"13:00 - 14:00", "14:00 - 14:45"];const grouped = times.map(time => {&nbsp; const [min, max] = time.split(' - ');&nbsp; const groupedData = array.filter(({dateTime}) => {&nbsp; &nbsp; const ts = dateTime.substring(9, 14);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; return ts >= min && ts < max;&nbsp; });&nbsp;&nbsp;&nbsp; return {time, groupedData};});console.log(grouped);

肥皂起泡泡

&nbsp; &nbsp;const array = [&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 05:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 05:21:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 06:31:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 06:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 07:34:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 07:55:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 08:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 09:16:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 09:46:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 10:36:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 11:47:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 11:55:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 12:37:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 14:27:33" },&nbsp; &nbsp; &nbsp; &nbsp; { item: "item", category: "category", dateTime: "19/05/23 14:37:33" },&nbsp; &nbsp; ];&nbsp; &nbsp; // And I have time periods, for ex:&nbsp; &nbsp; const times = ["05:45 - 06:00", "06:00 - 07:00",&nbsp; &nbsp; &nbsp; &nbsp; "07:00 - 08:00", "08:00 - 09:00", "09:00 - 10:00",&nbsp; &nbsp; &nbsp; &nbsp; "10:00 - 11:00", "11:00 - 12:00", "12:00 - 13:00",&nbsp; &nbsp; &nbsp; &nbsp; "13:00 - 14:00", "14:00 - 14:45"&nbsp; &nbsp; ];&nbsp; &nbsp; const result = [];&nbsp; &nbsp; times.forEach((time) => {&nbsp; &nbsp; &nbsp; &nbsp; const [min, max] = time.split(" - ");&nbsp; &nbsp; &nbsp; &nbsp; const groupedData = array.filter(({ dateTime }) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return `${min}:00`.localeCompare(dateTime.slice(-8)) <= 0 && `${max}:00`.localeCompare(dateTime.slice(-8)) >= 0;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; result.push({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; groupedData&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; console.log('@>result', result)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript