简体   繁体   中英

Correct warning : "Expected to return a value in arrow function array-callback-return "

I want to correct the warning

Expected to return a value in arrow function array-callback-return".

I tried adding return but the problem still exists.

listTaskWorkflow.forEach(element => {
    workflowDetail.department.departmentsList.find(department => {
        if (department.idDept === element.idDept) 
            element.idDept = department.departmentName;          
    })
});

find expects you to tell it whether the item it's calling you back with is the item you're looking for. To use it in that code, you'd leave the idDept check in the find but then use the array element it returns:

listTaskWorkflow.forEach(element => {
    const department = workflowDetail.department.departmentsList.find(department => department.idDept === element.idDept);
    if (department) {
        element.idDept = department.departmentName;          
    }
});

Another option, though, would be to use a for-of loop:

listTaskWorkflow.forEach(element => {
    for (const department of workflowDetail.department.departmentsList) {
        if (department.idDept === element.idDept) {
            element.idDept = department.departmentName;          
            break;
        }
    }
});

You could use for-of instead of forEach as well if you wanted:

for (const element of listTaskWorkflow) {
    for (const department of workflowDetail.department.departmentsList) {
        if (department.idDept === element.idDept) {
            element.idDept = department.departmentName;          
            break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM