आप 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
// ...
}); प्रत्येक के लिए . से एक अपवाद फेंकें . उदाहरण के लिए,
उदाहरण
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.
}