forEach 메소드를 중단할 수 없으며 루프 이스케이프를 제공하지 않습니다(예외 발생 제외).
대신 _.find from lodash와 같은 다른 기능을 사용할 수 있습니다. −
_.찾기 - 요소가 발견되면 루프에서 벗어납니다. 예를 들어,
예시
_.find([1, 2, 3, 4], (element) => {
// Check your condition here
if (element === 2) {
return true;
}
// Do what you want with the elements here
// ...
}); forEach에서 예외 발생 . 예를 들어,
예시
try {
[1, 2, 3, 4].forEach((element) => {
// Check your condition here
if (element === 2) {
throw new Error();
}
// Do what you want with the elements here
// ...
})
} catch (e) {
// Do nothing.
}