useEffect(() => {
if (status || group) {
setFilteredProjects(
projects.filter((project) => {
if (group !== "" && status !== "") {
return (
project.status === status && project.workgroup.name === group
);
} else {
return (
project.status === status || project.workgroup.name === group
);
}
})
);
} else {
setFilteredProjects(projects);
}
}, [status, group, projects]);
这段代码目前正在运行,但我觉得有一种更优雅的编写方式。
答案 0 :(得分:0)
你可以这样做
const filteredProjects = projects.filter((project) => {
return group !== "" && status !== ""
? project.status === status && project.workgroup.name === group
: project.status === status || project.workgroup.name === group;
});
setFilteredProjects(filteredProjects);