คุณไม่สามารถแยกจากแต่ละวิธี มันคัดลอกพฤติกรรมของ forEachmethod ดั้งเดิมและ forEach ดั้งเดิมไม่ได้จัดเตรียมให้เพื่อหลีกเลี่ยง theloop (นอกเหนือจากการส่งข้อยกเว้น)
คุณสามารถใช้ฟังก์ชันอื่นๆ เช่น −
-
_.find:แยกออกจากลูปเมื่อพบองค์ประกอบ ตัวอย่าง
_.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]).each((element) => {
// Check your condition here
if (element === 2) {
throw new Error();
}
// Do what you want with the elements here
// ...
})
}
catch (e) {
// Do nothing.
}