for each 中的返回语句不会停止执行 javascript



const checkAttendanceStudent = (attendance, std_id) => {
//check if the teacher make attendance meanwhile for this current session
let checkNow = attendance.find((element) => {
if (checkDateAttendance(element.date)) {
//check if the student exsit and checked first tiem in this current session
let checkInside = element.infoDto.find((studentAT) => {
if (parseInt(studentAT.id) === parseInt(std_id)) {
return true;
}
});
return checkInside;
}
});
return checkNow === undefined ? false : true;
};
当用户返回到停止时,如何解决foreach问题?---我用这种方式过滤,对吗?

您可以使用Array#someArray#every并返回一个布尔值来停止迭代。

选择取决于逻辑需要。

在您的代码下面使用some而不是find

const checkAttendanceStudent = (attendance, std_id) => 
attendance.some(({ date, infoDto }) => 
checkDateAttendance(date) &&
infoDto.some(({ id }) => +id === +std_id)
);

顺便说一句,checkAttendanceStudent的措辞具有误导性。最好使用引用函数结果的措辞,如has...is...表示布尔值。

用while循环和布尔变量替换Each以退出。这是处理这种情况的正确方法。不要使用退货。我知道";返回";它是创造出来的,我并不抗拒它的使用,但它必须在真正需要的地方使用。所以用这个逻辑更改你的代码

while( condition && isTimeToExit == true){
.....
//Where u see what u want set isTimeToExit to true
}

过滤器是好的,但如果你需要停止,你可能会看到其他循环。因为您已经达到了目标,并且想要退出使用标准while循环。

最新更新