To stop a for loop early in JavaScript, you use break:
const remSize = [];
let remData;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
let remIndex = -1; // Set a default if we don't find it
for (let i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
if (remSize[i].size === remData.size) {
remIndex = i;
break; // <=== breaks out of the loop early
}
}
That said, for this specific use case, you can use Array's findIndex (to find the entry's index) or find (to find the entry itself):
const remIndex = remSize.findIndex((entry) => entry.size === remData.size);
find stops the first time the callback returns a truthy value, returning the element that the callback returned the truthy value for (returns undefined if the callback never returns a truthy value):
const remEntry = remSize.find((entry) => entry.size === remData.size);
findIndex does the same thing, but returns the index of the entry the callback returned a truthy value for or -1 if it never does.
(There's also some function, but it wouldn't be appropriate here.)
More about looping in my other answer here.
Answer from T.J. Crowder on Stack OverflowTo stop a for loop early in JavaScript, you use break:
const remSize = [];
let remData;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
let remIndex = -1; // Set a default if we don't find it
for (let i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
if (remSize[i].size === remData.size) {
remIndex = i;
break; // <=== breaks out of the loop early
}
}
That said, for this specific use case, you can use Array's findIndex (to find the entry's index) or find (to find the entry itself):
const remIndex = remSize.findIndex((entry) => entry.size === remData.size);
find stops the first time the callback returns a truthy value, returning the element that the callback returned the truthy value for (returns undefined if the callback never returns a truthy value):
const remEntry = remSize.find((entry) => entry.size === remData.size);
findIndex does the same thing, but returns the index of the entry the callback returned a truthy value for or -1 if it never does.
(There's also some function, but it wouldn't be appropriate here.)
More about looping in my other answer here.
Use for of loop instead which is part of ES2015 release. Unlike forEach, we can use return, break and continue. See https://hacks.mozilla.org/2015/04/es6-in-depth-iterators-and-the-for-of-loop/
let arr = [1,2,3,4,5];
for (let ele of arr) {
if (ele > 3) break;
console.log(ele);
}
How can i stop for/loop if met condition
How do I end a For or While loop without using break?
How do I break out of a function while in a forEach loop?
Is there a way to exit a forEach loop early?
Videos
So I have this function that is supposed to go through a nodelist of divs being hovered and when clicked it should check if any of the squares were already checked. If they are I want to break out of the function but because i'm inside a forEach loop the return doesn't do anything. How can I check the list and break out of the function.