You can use try catch Throw an exception to skillfully stop forEach Traverse
Of course not in development During the interview Speak out At least it's also an extra item
// Right case Must use try catch It's all wrapped up forEach To stop
try {
[1,2,3,4,5,6].forEach(function(item, index){
console.log(item);
if(item === 3){
throw new Error(' prevent ');
}
});
} catch (error) {
console.log('error_ Block successful ', error);
}
forEach Contained in the try catch Can't succeed
// Failure cases
[1,2,3,4,5,6].forEach(function(item, index){
if(item === 3){
try {
throw new Error(' Stop failed ');
} catch (error) {
console.log('error', 'error_ Stop failed ');
}
}
});