我實現了一個condition2
取決於一個condition1
這樣的:條件依賴於另一個條件:有條件的情況
// condition1 comes from elsewhere
let condition2;
if(condition1){
condition2='task.done'
}else{
condition2='! task.done'
}
mappedTasks=tasks.map((task,idx)=>{
if(eval(condition2)){
return /* stuff */
}else{
return /* other stuff */
}
});
然而,功能eval
不能做的工作,我收到錯誤,如:
ReferenceError: task is not defined at eval
我想知道是否有人知道實現該條件條件的最佳方式是什麼。
現在的代碼工作:
let condition2;
if(condition1){
condition2=task=>task.done;
}else{
condition2=task=>!task.done;
}
mappedTasks=tasks.map((task,idx)=>{
if(condition2(task)){
return /* stuff */
}else{
return /* other stuff */
}
});
@ brso05因爲OP要評估是'task.done'或' !回調函數中的task.done'; 'task'是'.map()'回調的參數。 – Pointy
@積分是的。 – user3405291