loops and forEach

if i have for example ,
for(let i = 0; i<10; i++){

for(let j = 0; j < 10; j++){

if(j === 6) break;

}
}
for(let i = 0; i<10; i++){

for(let j = 0; j < 10; j++){

if(j === 6) break;

}
}
u can at any point i can stop the loop from doing any further loops using break but if I have something like,
arr1 = [1,2,3,4];
arr2 = [6,7,3,9];

arr1.forEach( itm1 => {

arr2.forEach( itm2 => {

if( itm1 === itm2 )

//i would want it to stop checking for the rest of the items in arr2

}
}
arr1 = [1,2,3,4];
arr2 = [6,7,3,9];

arr1.forEach( itm1 => {

arr2.forEach( itm2 => {

if( itm1 === itm2 )

//i would want it to stop checking for the rest of the items in arr2

}
}
is it possible to add something similar to break in the if condition that'll stop the forEach from doung further operation on the items from arr2??
5 Replies
Jochem
Jochem14mo ago
there is not you can use for/of if you need to break, it's simpler than a regular for loop https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
Chris Bolson
Chris Bolson14mo ago
you could force a break by changing the length of the arr1 once you find a match but of course that will mutate your array.
arr1.forEach( itm1 => {
arr2.forEach( (itm2,index) => {
if( itm1 === itm2 ){
//console.log("break here", itm1, itm2);
arr1.length = index+1;
}
});
});
arr1.forEach( itm1 => {
arr2.forEach( (itm2,index) => {
if( itm1 === itm2 ){
//console.log("break here", itm1, itm2);
arr1.length = index+1;
}
});
});
13eck
13eck14mo ago
As Jochem said, .forEach() will iterate through all elements in the array, no breaking. The for...of loop is what you're looking for:
const arr1 = [1,2,3,4];
const arr2 = [6,7,3,9];

for (const item1 of arr1) {
for (const item2 of arr2) {
if (item1 == item2) return console.log(`FOUND ${item2}!`);
}
}
const arr1 = [1,2,3,4];
const arr2 = [6,7,3,9];

for (const item1 of arr1) {
for (const item2 of arr2) {
if (item1 == item2) return console.log(`FOUND ${item2}!`);
}
}
The return statement will kill the for loops. If we add a console.log in there, we see that it does indeed stop at 3 == 3:
const arr1 = [1,2,3,4];
const arr2 = [6,7,3,9];

for (const item1 of arr1) {
for (const item2 of arr2) {
console.log(`${item1} == ${item2}? ${item1 == item2}`);
if (item1 == item2) return console.log(`FOUND MATCH!`);
}
}
const arr1 = [1,2,3,4];
const arr2 = [6,7,3,9];

for (const item1 of arr1) {
for (const item2 of arr2) {
console.log(`${item1} == ${item2}? ${item1 == item2}`);
if (item1 == item2) return console.log(`FOUND MATCH!`);
}
}
Produces:
"1 == 6? false"
"1 == 7? false"
"1 == 3? false"
"1 == 9? false"
"2 == 6? false"
"2 == 7? false"
"2 == 3? false"
"2 == 9? false"
"3 == 6? false"
"3 == 7? false"
"3 == 3? true"
"FOUND MATCH!"
"1 == 6? false"
"1 == 7? false"
"1 == 3? false"
"1 == 9? false"
"2 == 6? false"
"2 == 7? false"
"2 == 3? false"
"2 == 9? false"
"3 == 6? false"
"3 == 7? false"
"3 == 3? true"
"FOUND MATCH!"
Moral of the story: the .filter(), .map(), .reduce(), .forEach(), and other array methods work on every element of the array provided, that's their job. If you want to not do a thing on every element in an array you need a for...of loop.
Jochem
Jochem14mo ago
this doesn't work https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#description
Note, however, that the length of the array is saved before the first invocation of callbackFn.
glutonium
glutonium14mo ago
yaa that's what I did actually xD but it doesn't sound very good ahaha i feel like it might create more complexity yaa i got it..tnx❤️