gpt4 book ai didi

javascript - TS 中 "every"函数的 TypeError

转载 作者:行者123 更新时间:2023-12-04 19:08:09 25 4
gpt4 key购买 nike

我正在开发一个项目,该项目根据工作计划和用户的个人可用性过滤工作。

const jobs: JobRecord[] = [
{
"storeName": "Highwire Coffee Company",
"schedule": [
"Tuesday",
"Thursday",
],
...
},
{
"storeName": "Blue Bottle Coffee",
"rid": "recNgeUMcYhWUxw8b",
"schedule": [
"Thursday",
],
...
},
]

const availability: Availability = {
"friday": false,
"monday": false,
"thursday": true,
"tuesday": false,
"wednesday": true,
}

例如,鉴于上面的这些输入,我应该创建一个函数,只返回商店名称为“Blue Bottle Coffee”的作业。
为此,我编写了这两个函数。

  findAvailableDays (availability: Availability){
var availDays: string[];
availDays = [];
for (const [day, avail] of Object.entries(availability)){
if (avail){
availDays.push(day);
}
}
return availDays;
};

filterJobs = (jobs: JobRecord[], availability: Availability): void => {
// Step 0: Clone the jobs input
const newJobs: JobRecord[] = cloneDeep(jobs);
console.log(newJobs, availability);

// Step 1: Remove jobs where the schedule doesn't align with the users' availability.
for (const[store, schedule] of Object.entries(jobs)){
var availDays: string[];
availDays = this.findAvailableDays(availability);
const checkIncludes = (currentValue) => availDays.includes(currentValue);
let checker = (availDays, schedule) => schedule.every(checkIncludes(checkIncludes));
if(!checker(availDays, schedule)){
delete newJobs[store];
}
}
// Step 2: Save into state
this.setState({ jobs: newJobs });
};

但是,我在 filterJobs 中的 every() 函数总是遇到错误(如下所示)。我已经检查了 every() 是否接受函数作为参数(我确定),并且适用于数组(我相信 schedule 应该是一个数组,给出的例子)。我在这里做错了什么/错过了什么?我应该尝试找到另一种方法来解决这个问题吗?
enter image description here

最佳答案

schedule不是数组,而是 JobRecord .您可能是这个意思,scheduleJobRecord 解构:

for (const [store, {schedule}] of Object.entries(jobs)) {
但是,编写整个内容的更好方法是不按索引删除 - 使用 filter反而:
filterJobs = (jobs: JobRecord[], availability: Availability): void => {
const availDays = this.findAvailableDays(availability);

const filteredJobs = jobs.filter(({schedule}) =>
schedule.every(day => availDays.includes(day)));

this.setState({ jobs: filteredJobs });
};
看起来您可能需要考虑 availability 的属性名称之间的大小写差异。和 schedule 的元素, 顺便一提。

关于javascript - TS 中 "every"函数的 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63856411/

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