[1,2,3].forEach(function(el) {
if(el === 1) break;
});
如何在JavaScript中使用新的forEach方法实现这一点?我尝试过返回;,return false;和断裂。break崩溃,return只会继续迭代。
[1,2,3].forEach(function(el) {
if(el === 1) break;
});
如何在JavaScript中使用新的forEach方法实现这一点?我尝试过返回;,return false;和断裂。break崩溃,return只会继续迭代。
当前回答
尝试“查找”:
var myCategories = [
{category: "start", name: "Start", color: "#AC193D"},
{category: "action", name: "Action", color: "#8C0095"},
{category: "exit", name: "Exit", color: "#008A00"}
];
function findCategory(category) {
return myCategories.find(function(element) {
return element.category === category;
});
}
console.log(findCategory("start"));
// output: { category: "start", name: "Start", color: "#AC193D" }
其他回答
这不是最有效的,因为你仍然循环所有元素,但我认为这可能值得考虑非常简单的:
let keepGoing = true;
things.forEach( (thing) => {
if (noMore) keepGoing = false;
if (keepGoing) {
// do things with thing
}
});
如果您想使用Dean Edward的建议并抛出StopIteration错误以跳出循环而不必捕获错误,则可以使用以下函数(最初来自此处):
// Use a closure to prevent the global namespace from be polluted.
(function() {
// Define StopIteration as part of the global scope if it
// isn't already defined.
if(typeof StopIteration == "undefined") {
StopIteration = new Error("StopIteration");
}
// The original version of Array.prototype.forEach.
var oldForEach = Array.prototype.forEach;
// If forEach actually exists, define forEach so you can
// break out of it by throwing StopIteration. Allow
// other errors will be thrown as normal.
if(oldForEach) {
Array.prototype.forEach = function() {
try {
oldForEach.apply(this, [].slice.call(arguments, 0));
}
catch(e) {
if(e !== StopIteration) {
throw e;
}
}
};
}
})();
上述代码将使您能够运行以下代码,而无需执行自己的try-catch子句:
// Show the contents until you get to "2".
[0,1,2,3,4].forEach(function(val) {
if(val == 2)
throw StopIteration;
alert(val);
});
需要记住的一点是,如果Array.prototype.forEach函数已经存在,则只会更新它。如果它还不存在,它将不会修改它。
简短回答:用于。。。为此中断或更改代码以避免中断forEach。不要使用.some()或.every()来模拟。。。打破重写代码以避免。。。中断循环,或用于。。。打破每次你使用这些方法。。。上帝杀死了小猫。
长答案:
.some()和.every()都返回布尔值,如果有任何元素的传递函数返回true,.some(返回true,如果有元素的传递的函数返回false,则every返回false。这就是函数的含义。使用函数来实现它们并不意味着什么,这比使用表来布局而不是CSS要糟糕得多,因为这会让所有阅读代码的人感到沮丧。
此外,使用这些方法的唯一可能方法是。。。break替代方法是产生副作用(在.some()回调函数之外更改一些vars),这与for。。。打破
因此,使用.some()或.every()作为。。。断环替代方案并没有副作用,这对。。。打破,这是令人沮丧的,所以这不是更好的。
您可以随时重写代码,这样就不需要。。。打破您可以使用.filter()过滤数组,也可以使用.slice()等拆分数组,然后使用.forEach()或.map()处理该部分数组。
没有内置的功能可以为Each进行破解。要中断执行,必须抛出某种异常。
var BreakException={};尝试{[1,2,3]对于每个(函数(el){控制台日志(el);如果(el==2)抛出BreakException;});}捕获(e){如果(e!==BreakException)抛出e;}
JavaScript异常并不漂亮。如果你真的需要打破传统的for循环,那么它可能更合适。
使用数组#some
而是使用Array#some:
[1,2,3]一些(函数(el){控制台日志(el);返回el==2;});
这之所以有效,是因为只要按数组顺序执行的任何回调返回true,就会立即返回true,从而缩短其余回调的执行。
有些,它的逆every(返回false时停止)和forEach都是ECMAScript第五版方法,需要在缺少它们的浏览器上添加到Array.prototype中。
使用数组#间隔
[1,2,3]每(v=>{如果(v>2){return false//“break”}控制台日志(v);return true//如果不中断,则必须返回true});
如果您想保持forEach语法,这是一种保持其高效的方法(尽管不如常规for循环好)。立即检查一个变量,该变量是否知道您是否想要脱离循环。
此示例使用匿名函数来围绕forEach创建函数范围,您需要存储已完成的信息。
(函数(){var element=document.getElementById('打印结果');var done=假;[1,2,3,4].forEach(功能(项目){如果(完成){return;}var text=document.createTextNode(项);element.appendChild(文本);如果(项==2){done=真;回来}});})();<div id=“printed result”></div>
我的两分钱。