对于这两种情况(fixAllJobs,logAllJobs),如何优化processAllJobs函数。
processAllJobs = (channel) => {
const jobsCopy = [...this.getState().jobs];
let validJobs = [];
jobsCopy.forEach((job) => {
if (channel === 'fixJobs') {
if (job.processStatus !== ERROR && job.processStatus !== FIXED) {
job.processStatus = IN_PROCESS;
job.progress = 0;
validJobs.push(job);
}
} else {
if (job.processStatus !== ERROR) {
job.processStatus = IN_PROCESS;
job.progress = 0;
validJobs.push(job);
}
}
});
this.setState({isRunning: true, jobs: jobsCopy});
ipcRenderer.send(channel, validJobs);
};
fixAllJobs = () => {
this.processAllJobs('fixJobs');
};
logAllJobs = () => {
this.processAllJobs('logJobs');
};
我只是想出了这个,但这完全是胡说。在if条件下编写三元运算符是一种好方法吗?
if (channel === 'fixJobs' ? job.processStatus !== ERROR
&& job.processStatus !== FIXED : job.processStatus !== ERROR) {
job.processStatus = IN_PROCESS;
job.progress = 0;
validJobs.push(job);
}
最佳答案
您希望仅当通道为job.processStatus !== FIXED
时才检查条件fixJobs
,可以编写以下条件来实现job.processStatus !== ERROR && (channel !== 'fixJobs' || job.processStatus !== FIXED)