尝试为我正在构建的项目构建过滤器。我想通过JSON解析我从后端API得到,过滤出基于"位置"的职位;并显示它
useEffect(async () => {
let condition = await getJobs();
switch (jobCondition) {
case 'On-site': condition.filter((condition.jobLocation === 'in_person'));
break;
case 'Remote': condition.filter((condition.jobLocation === 'remote'));
break;
case 'Hybrid': condition.filter((condition.jobLocation === 'hybrid'));
break;
default:
condition = null;
}
if (condition != null) {
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: datePosted || null,
conditions: condition.data,
});
history.push({ pathname: '/', search: `${params.toString()}` });
return;
}
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: null,
conditions: null,
});
history.push({ pathname: '/', search: `${params.toString()}` });
}, [jobCondition]);
我显然在这里做错了,因为它甚至没有改变我的URL参数。
您需要传递一个带一个参数作为第一个参数的回调函数,然后返回一个布尔值:
condition.filter((con) => con.jobLocation === 'in_person');
顺便说一下,如果您想获得过滤后的数组,您需要重新分配condition
或将其分配给另一个变量。