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;
};
How to resolve foreach problem when iuse return to stopped it?
--- i use filter in this way, it is correct?
CodePudding user response:
You could take Array#some
or Array#every
and return a boolean value to stop the iteration.
The selection depends on the logical need.
Below your code with some
instead of find
.
const checkAttendanceStudent = (attendance, std_id) =>
attendance.some(({ date, infoDto }) =>
checkDateAttendance(date) &&
infoDto.some(({ id }) => id === std_id)
);
BTW, the wording of checkAttendanceStudent
is misleading. Better is to use a wording which refects the result of the function, like has...
or is...
for a boolean value.
CodePudding user response:
Substitute forEach with while loop and boolean variabile for exit. It's the right way for handle this situation. Don't use return. I know "return" it was created and I do not resist its use, however it must be used where it is really needed. So change your piece of code with this logic
while( condition && isTimeToExit == true){
.....
//Where u see what u want set isTimeToExit to true
}
Filter is good, but if you need to stop probably you can see other loops. Because you have reached your goal and you want to quit use standard while loop.