I have a function with nested loops, first is an array loop and inside a forEach loop.
const myFunction = (data: string[][]): boolean => {
for (const row of data) {
row.forEach((item, i) => {
if (!something) {
return false;
}
if (
!somethingelse
) {
return false;
}
return undefined;
});
}
return true;
};
Whenever I return false I would like that myFunction
will return false, and only at the end if false wasn't returned, I'd like to return true. However to my understanding since it's nested loops, When I return false, I'm actually breaking out of the loop.
The only solution I thought about is to declare a variable and set it to "false" and then return the result at the end of all loops. However I'm finding that this might not be a good solution since es-lint complains when trying to change variables declared outside of the loop (can cause memory leaks). Is there another way without creating a variable, is there a way to return from a function without solely break out of the loop?