Several ways to interrupt the forEach loop in js

1. Use the Array.prototype.some() method instead

The some() method stops the loop when it finds the first matching element.

For example:

let array = [1, 2, 3, 4, 5];
array.some(function(element, index, array) {
    if (element === 3) {
        console.log("Found 3 at index " + index);
        return true;
    }
});

The above code stops the loop when it finds the first element that meets the criteria (ie 3).

2. Use the Array.prototype.every() method instead

let array = [1, 2, 3, 4, 5];
let stop = array.every(function(element) {
    console.log(element);
    if (element === 3) {
        console.log("Found 3 at index ");
        return false;
    }
    return true;
});

The above code stops the loop when it finds the first element that meets the criteria (ie 3).

Please note that the elements found by this method will not be returned and need to be handled in the callback.

3. Use a for loop instead

let array = [1, 2, 3, 4, 5];
for(let i = 0; i < array.length; i++) {
    if (array[i] === 3) {
        console.log("Found 3 at index " + i);
        break;
    }
}

The above code will also stop the loop when it finds the first eligible element (ie 3).

4. Use the try-catch structure

You can use a throw statement inside the forEach loop to break out of the loop and a catch statement outside to catch the exception.

For example:

let array = [1, 2, 3, 4, 5];
try {
    array.forEach(function(element, index, array) {
        if (element === 3) {
            console.log("Found 3 at index " + index);
            throw "StopIteration";
        }
    });
} catch (e) {
    if (e !== "StopIteration") throw e;
}

The above code stops the loop when it finds the first element that meets the criteria (ie 3).

Note that breaking the forEach loop with a throw statement can make the code more complex and prone to errors. Therefore, if possible, the aforementioned Array.prototype.some() or for loop should be used instead.

5. Use a custom iterator function

let array = [1, 2, 3, 4, 5];

function forEach(array, callback) {
    for (let i = 0; i < array.length; i++) {
        callback(array[i], i, array);
        if (array[i] === 3) {
            console.log("Found 3 at index " + i);
            break;
        }
    }
}

forEach(array, function(element, index, array) {
    console.log(element);
});

The above code stops the loop when it finds the first element that meets the criteria (ie 3).

Although this method is not concise enough, it can add new functions without changing the original forEach function.

6. Use the Array.prototype.find() or Array.prototype.findIndex() method instead

The find() method returns the first element that matches the condition and stops the loop.

For example:

let array = [1, 2, 3, 4, 5];
let found = array.find(function(element) {
    return element === 3;
});
console.log(found);

The above code stops the loop when it finds the first element that matches the criteria (ie 3) and returns that element.

The Array.prototype.findIndex() method will return the index of the first element after finding the condition and stop the loop.

For example:

let array = [1, 2, 3, 4, 5];
let index = array.findIndex(function(element) {
    return element === 3;
});
console.log(index);

The above code stops the loop when it finds the first element that matches the criteria (ie 3) and returns the index of that element.

Use the Array.prototype.find() or Array.prototype.findIndex() method to find the first element that meets the criteria in a forEach loop and stop the loop. These two methods return the element or index after finding the element that meets the conditions, instead of returning a Boolean value again or using the throw statement to interrupt the loop like some() method or for loop.

In short, the main method is to replace the interruption of the forEach loop by other means, only method 4 uses the try-catch structure to actually interrupt the forEach loop.

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/131103069