我有一个角度 foreach 循环,如果我匹配一个值,我想从循环中中断。以下代码不起作用。
angular.forEach([0,1,2], function(count){
if(count == 1){
break;
}
});
我怎样才能得到这个?
我有一个角度 foreach 循环,如果我匹配一个值,我想从循环中中断。以下代码不起作用。
angular.forEach([0,1,2], function(count){
if(count == 1){
break;
}
});
我怎样才能得到这个?
angular.forEach
循环不能在条件匹配时中断。
我个人的建议是使用NATIVE FOR循环而不是angular.forEach
.
NATIVE FOR 循环比其他 for 循环快90%左右。
在角度使用 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.');
}
没有办法做到这一点。请参阅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;
}
}
});
请使用 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.
一些例子:
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
ary.some(function (value, index, _ary) {
console.log(index + ": " + value);
return value === "JavaScript";
});
每个示例:
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
ary.every(function(value, index, _ary) {
console.log(index + ": " + value);
return value.indexOf("Script") > -1;
});
查找更多信息
http://www.jsnoob.com/2013/11/26/how-to-break-the-foreach/
使用数组一些方法
var exists = [0,1,2].some(function(count){
return count == 1
});
存在将返回 true,您可以将其用作函数中的变量
if(exists){
console.log('this is true!')
}
据我所知,Angular 不提供这样的功能。您可能需要为此使用下划线find()
函数(它基本上是一个 forEach,一旦函数返回 true,它就会跳出循环)。
如果您将 jQuery(因此不是 jqLite)与 AngularJS 结合使用,您可以使用 $.each 进行迭代 - 它允许基于布尔返回值表达式进行中断和继续。
JSF中:
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
}
});
笔记:
尽管使用 jQuery 还不错,但 MDN 推荐使用原生Array.some或Array.every函数,您可以在原生forEach文档中阅读:
“没有办法停止或中断 forEach 循环。解决方案是使用 Array.every 或 Array.some”
以下示例由 MDN 提供:
数组.一些:
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
数组.每个:
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
具体来说,你可以退出一个forEach
循环,在任何地方都可以抛出异常。
try {
angular.forEach([1,2,3], function(num) {
if (num === 2) throw Error();
});
} catch(e) {
// anything
}
但是,最好使用其他库或实现自己的函数,find
在这种情况下是一个函数,因此您的代码是最高级的。
试试这个作为休息;
angular.forEach([0,1,2], function(count){
if(count == 1){
return true;
}
});
正如其他答案所述,Angular 不提供此功能。但是 jQuery 可以,如果你已经加载了 jQuery 和 Angular,你可以使用
jQuery.each ( array, function ( index, value) {
if(condition) return false; // this will cause a break in the iteration
})
通常没有办法打破javascript中的“每个”循环。通常可以做的是使用“短路”方法。
array.forEach(function(item) {
// if the condition is not met, move on to the next round of iteration.
if (!condition) return;
// if the condition is met, do your logic here
console.log('do stuff.')
}
break
在角度 forEach 中是不可能实现的,我们需要修改 forEach 来做到这一点。
$scope.myuser = [{name: "Ravi"}, {name: "Bhushan"}, {name: "Thakur"}];
angular.forEach($scope.myuser, function(name){
if(name == "Bhushan") {
alert(name);
return forEach.break();
//break() is a function that returns an immutable object,e.g. an empty string
}
});
你可以使用这个:
var count = 0;
var arr = [0,1,2];
for(var i in arr){
if(count == 1) break;
//console.log(arr[i]);
}
var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
var keepGoing = true;
ary.forEach(function(value, index, _ary) {
console.log(index)
keepGoing = true;
ary.forEach(function(value, index, _ary) {
if(keepGoing){
if(index==2){
keepGoing=false;
}
else{
console.log(value)
}
}
});
});
$scope.arr = [0, 1, 2];
$scope.dict = {}
for ( var i=0; i < $scope.arr.length; i++ ) {
if ( $scope.arr[i] == 1 ) {
$scope.exists = 'yes, 1 exists';
break;
}
}
if ( $scope.exists ) {
angular.forEach ( $scope.arr, function ( value, index ) {
$scope.dict[index] = value;
});
}
我宁愿通过返回来做到这一点。将循环部分放在私有函数中,并在您想中断循环时返回。
这个例子有效。尝试一下。
var array = [0,1,2];
for( var i = 0, ii = array.length; i < ii; i++){
if(i === 1){
break;
}
}
我意识到这很旧,但是数组过滤器可以满足您的需要:
var arr = [0, 1, 2].filter(function (count) {
return count < 1;
});
然后可以运行arr.forEach
其他数组函数。
我意识到,如果你打算完全减少循环操作,这可能不会达到你想要的效果。为此,您最好使用while
.
我会使用return
而不是break
.
angular.forEach([0,1,2], function(count){
if(count == 1){
return;
}
});
奇迹般有效。
使用 Return 来中断循环。
angular.forEach([0,1,2], function(count){
if(count == 1) {
return;
}
});
onSelectionChanged(event) {
let selectdata = event['api']['immutableService']['gridOptionsWrapper']['gridOptions']['rowData'];
let selected_flag = 0;
selectdata.forEach(data => {
if (data.selected == true) {
selected_flag = 1;
}
});
if (selected_flag == 1) {
this.showForms = true;
} else {
this.showForms = false;
}
}
只需添加 $index 并执行以下操作:
angular.forEach([0,1,2], function(count, $index) {
if($index !== 1) {
// do stuff
}
}