Sab*_*kar 249 javascript-framework angularjs
我有一个角度的foreach循环,如果我匹配一个值,我想从循环中断.以下代码不起作用.
angular.forEach([0,1,2], function(count){
if(count == 1){
break;
}
});
Run Code Online (Sandbox Code Playgroud)
我怎么能得到这个?
Nis*_*ani 290
该angular.forEach循环不能在条件匹配打破.
我个人的建议是使用NATIVE FOR循环而不是angular.forEach.
NATIVE FOR循环比其他循环快约90%.

在ANGULAR中使用FOR循环:
var numbers = [0, 1, 2, 3, 4, 5];
for (var i = 0, len = numbers.length; i < len; i++) {
if (numbers[i] === 1) {
console.log('Loop is going to break.');
break;
}
console.log('Loop will continue.');
}
Run Code Online (Sandbox Code Playgroud)
dnc*_*253 258
没有办法做到这一点.请参阅https://github.com/angular/angular.js/issues/263.根据你正在做的事情,你可以使用布尔值来进入循环体.就像是:
var keepGoing = true;
angular.forEach([0,1,2], function(count){
if(keepGoing) {
if(count == 1){
keepGoing = false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
Neo*_*jay 22
请使用ForEach的部分或全部实例,
Array.prototype.some:
some is much the same as forEach but it break when the callback returns true
Array.prototype.every:
every is almost identical to some except it's expecting false to break the loop.
Run Code Online (Sandbox Code Playgroud)
一些例子:
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
ary.some(function (value, index, _ary) {
console.log(index + ": " + value);
return value === "JavaScript";
});
Run Code Online (Sandbox Code Playgroud)
每个例子:
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
ary.every(function(value, index, _ary) {
console.log(index + ": " + value);
return value.indexOf("Script") > -1;
});
Run Code Online (Sandbox Code Playgroud)
查找更多信息
http://www.jsnoob.com/2013/11/26/how-to-break-the-foreach/
小智 17
使用Array Some Method
var exists = [0,1,2].some(function(count){
return count == 1
});
Run Code Online (Sandbox Code Playgroud)
exists将返回true,您可以将其用作函数中的变量
if(exists){
console.log('this is true!')
}
Run Code Online (Sandbox Code Playgroud)
据我所知,Angular没有提供这样的功能.你可能想要使用下划线的find()函数(它基本上是一个forEach,一旦函数返回true就会突破循环).
如果将jQuery(因此不是jqLite)与AngularJS结合使用,则可以使用$ .each进行迭代 - 这允许基于布尔返回值表达式进行断开和继续.
的jsfiddle:
使用Javascript:
var array = ['foo', 'bar', 'yay'];
$.each(array, function(index, element){
if (element === 'foo') {
return true; // continue
}
console.log(this);
if (element === 'bar') {
return false; // break
}
});
Run Code Online (Sandbox Code Playgroud)
注意:
虽然使用jQuery也不错,MDN推荐使用本机Array.some或Array.every函数,因为您可以在native forEach文档中阅读:
"没有办法停止或打破forEach循环.解决方案是使用Array.every或Array.some"
以下示例由MDN提供:
Array.some:
function isBigEnough(element, index, array){
return (element >= 10);
}
var passed = [2, 5, 8, 1, 4].some(isBigEnough);
// passed is false
passed = [12, 5, 8, 1, 4].some(isBigEnough);
// passed is true
Run Code Online (Sandbox Code Playgroud)
Array.every:
function isBigEnough(element, index, array){
return (element >= 10);
}
var passed = [12, 5, 8, 130, 44].every(isBigEnough);
// passed is false
passed = [12, 54, 18, 130, 44].every(isBigEnough);
// passed is true
Run Code Online (Sandbox Code Playgroud)
具体来说,您可以退出forEach循环,任何地方,抛出异常.
try {
angular.forEach([1,2,3], function(num) {
if (num === 2) throw Error();
});
} catch(e) {
// anything
}
Run Code Online (Sandbox Code Playgroud)
但是,最好是使用其他库或实现自己的函数,find在这种情况下是一个函数,因此您的代码是最高级的.
小智 5
试试这个作为休息;
angular.forEach([0,1,2], function(count){
if(count == 1){
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
304708 次 |
| 最近记录: |