当前位置:网站首页>Interrupt array Foreach method [js implementation]

Interrupt array Foreach method [js implementation]

2022-06-23 03:00:00 It workers

Realization principle

There is no built-in method to implement interrupts forEach, If we want to implement interrupts forEach, We can do this by throwing an exception .

Implementation code

var BreakException = {};

try {
  [1, 2, 3].forEach(function(el) {
    console.log(el);
    if (el === 2) throw BreakException;
  });
} catch (e) {
  if (e !== BreakException) throw e;
}
原网站

版权声明
本文为[It workers]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201260841175933.html