【问题标题】:javascript find() returns true however item is not included in the arrayjavascript find() 返回 true 但是项目不包含在数组中
【发布时间】:2021-10-16 16:07:33
【问题描述】:

我有一个简单的辅助函数来根据给定日期在日期数组中查找日期,在此函数中我将日期名称转换为日期编号,然后我想查找具有给定日期编号的所有日期:

function findDatesByWeekday(date, weekdayArr) {
    const dayNumbers = weekdayArr.map(({ day }) => textToDayNumber(day));
    return dayNumbers.find(n => {
        console.log(
            `n: ${n}, getDay: ${new Date(date).getDay()} result: ${new Date(
                date
            ).getDay() === n} date: ${date}`
        );
        return new Date(date).getDay() === n;
    });
}

但是,虽然return 语句的结果为真,但它没有找到星期日。

n: 6, getDay: 6 result: true date: Sat Oct 16 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 6, getDay: 0 result: false date: Sun Oct 17 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 0, getDay: 0 result: true date: Sun Oct 17 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 6, getDay: 6 result: true date: Sat Oct 23 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 6, getDay: 0 result: false date: Sun Oct 24 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 0, getDay: 0 result: true date: Sun Oct 24 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 6, getDay: 6 result: true date: Sat Oct 30 2021 12:00:00 GMT+0200 (Central European Summer Time)
n: 6, getDay: 0 result: false date: Sun Oct 31 2021 12:00:00 GMT+0100 (Central European Standard Time)
n: 0, getDay: 0 result: true date: Sun Oct 31 2021 12:00:00 GMT+0100 (Central European Standard Time)

更重要的是,当我要求五个日期时,它产生了不需要的输出,它返回五个星期六而不是三个星期六和两个星期日。

0: Sat Oct 16 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
1: Sat Oct 23 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
2: Sat Oct 30 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
3: Sat Nov 06 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
4: Sat Nov 13 2021 12:00:00 GMT+0100 (Central European Standard Time) {}

每隔一周的一天它工作得很好,只是星期天,所以我很确定它归零是一个虚假的值,我知道如何修复它下面的功能按预期工作

function findDatesByWeekday(date, weekdayArr) {
    const dayNumbers = weekdayArr.map(({ day }) => textToDayNumber(day));
    return dayNumbers.includes(new Date(date).getDay());
}

我只是想了解原因:) 谢谢

您好是您所要求的问题编辑的开始。你是对的,我很抱歉我应该默认包含上下文。我不使用过滤器,因为我在过滤器方法中使用这个助手作为谓词

newHighlightedDays = benchmark.filter(date =>
   findDatesByWeekday(date, weekDaysWithTime))
.filter((_, i) => i < lessonCount);

weekdaysWithTime 是一个对象数组,如下所示:

{
day: "Piątek",
time: {h: 12, m: 30}
},
{
day: "Sobota",
time: {h: 12, m: 30}
},
{
day: "Niedziela",
time: {h: 12, m: 30}
}

而textToDayNumber就是这个函数:

export function textToDayNumber(text) {
    switch (text.toLowerCase()) {
        case "niedziela":
            return 0;
        case "poniedziałek":
            return 1;
        case "wtorek":
            return 2;
        case "środa":
            return 3;
        case "czwartek":
            return 4;
        case "piątek":
            return 5;
        case "sobota":
            return 6;
        default:
            throw new Error("invalid day name");
    }
}

我确信这不是时区问题,因为它本身比较的不是日期,而是解析为日期编号的工作日名称与 react-day-picker 中的日期,这是重现代码的基准数组示例:

Tue Oct 19 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Wed Oct 20 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Thu Oct 21 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Fri Oct 22 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Sat Oct 23 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Sun Oct 24 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Mon Oct 25 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Tue Oct 26 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Wed Oct 27 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Thu Oct 28 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Fri Oct 29 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Sat Oct 30 2021 12:00:00 GMT+0200 (Central European Summer Time) {}
Sun Oct 31 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Mon Nov 01 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Tue Nov 02 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Wed Nov 03 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Thu Nov 04 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Fri Nov 05 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Sat Nov 06 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Sun Nov 07 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Mon Nov 08 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Tue Nov 09 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Wed Nov 10 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Thu Nov 11 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Fri Nov 12 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Sat Nov 13 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Sun Nov 14 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Mon Nov 15 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Tue Nov 16 2021 12:00:00 GMT+0100 (Central European Standard Time) {}
Wed Nov 17 2021 12:00:00 GMT+0100 (Central European Standard Time) {}

谢谢! :)

【问题讨论】:

  • How do I ask a good question?: “包含足够的代码以允许其他人重现问题。有关此问题的帮助,请阅读How to create a Minimal, Complete, and Verifiable example。”
  • “所以我很确定它归零是一个虚假值” - 确实如此,但您的 .find() 版本返回一个实际的布尔值 (new Date(date).getDay() === n ) 而不是数字
  • 您确定这不是时区问题而不是日期问题吗?
  • @Andreas .find 接受一个应该返回布尔值的回调。当您返回 true 时,这意味着该项目应包含在结果中。 w3schools.com/jsref/jsref_find.asp 但是 find 只返回条件为真的第一个项目。

标签: javascript arrays reactjs date datetime


【解决方案1】:

不确定我是否理解了这个问题:

然后我想查找具有给定日期编号的所有日期:

find 仅返回第一个匹配项。使用filter 返回匹配项的数组。

function findDatesByWeekday(date, weekdayArr) {
    const dayNumbers = weekdayArr.map(({ day }) => textToDayNumber(day));
    return dayNumbers.filter(n => {
        console.log(
            `n: ${n}, getDay: ${new Date(date).getDay()} result: ${new Date(
                date
            ).getDay() === n} date: ${date}`
        );
        return new Date(date).getDay() === n;
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2018-06-02
    • 1970-01-01
    • 2018-07-09
    • 2012-12-01
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    相关资源
    最近更新 更多