각 JS 브레이크
각도 포어치 루프가 있는데 값이 일치하면 루프에서 벗어나고 싶습니다.다음 코드는 동작하지 않습니다.
angular.forEach([0,1,2], function(count){
if(count == 1){
break;
}
});
어떻게 구하지?
그angular.forEach조건 일치 시 루프가 끊어질 수 없습니다.
제 개인적인 조언은 NATIVE FOR 루프를 사용하는 것입니다.angular.forEach.
NATIVE FOR 루프는 다른 루프보다 약 90% 더 빠릅니다.

각진 루프에 사용:
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
});
exists는 true를 반환합니다.이것을 함수의 변수로 사용할 수 있습니다.
if(exists){
console.log('this is true!')
}
제가 알기로는 앵글은 그런 기능을 제공하지 않습니다.밑줄의find()이를 위한 함수(기본적으로 함수가 true를 반환하면 루프에서 분리되는 forEach)입니다.
jQuery(jqLite가 아님)를 Angular와 함께 사용하는 경우JS는 $.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
}
});
주의:
jQuery를 사용하는 것도 나쁘지 않지만 MDN에서는 네이티브 Array.some 또는 Array.every 함수를 모두 권장합니다.각 매뉴얼은 네이티브에서 참조할 수 있습니다.
"각 루프를 정지하거나 차단할 방법은 없습니다.해결책은 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
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
구체적으로는, 를 종료할 수 있습니다.forEach루프, 어느 장소에서도 예외가 발생합니다.
try {
angular.forEach([1,2,3], function(num) {
if (num === 2) throw Error();
});
} catch(e) {
// anything
}
단, 다른 라이브러리를 사용하거나 자신의 기능인 a를 구현하는 것이 좋습니다.find이 경우 가장 높은 수준의 코드를 사용할 수 있습니다.
이것을 휴식 삼아 사용해 보세요.
angular.forEach([0,1,2], function(count){
if(count == 1){
return true;
}
});
다른 응답자가 말한 것처럼 Angular는 이 기능을 제공하지 않습니다.단, jQuery는 실행되며, 각도뿐만 아니라 jQuery를 로드한 경우에는 를 사용할 수 있습니다.
jQuery.each ( array, function ( index, value) {
if(condition) return false; // this will cause a break in the iteration
})
http://api.jquery.com/jquery.each/ 를 참조해 주세요.
보통 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각도로는 달성할 수 없습니다.각각으로 수정해야 합니다.
$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 arr = [0, 1, 2].filter(function (count) {
return count < 1;
});
'보다 낫다'를 하면 됩니다.arr.forEach기타 어레이 기능.
만약 당신이 루프 조작을 완전히 줄이려고 한다면, 이것은 당신이 원하는 대로 되지 않을 것입니다. 위해서는, 「 」, 「 」를 사용하는 좋습니다.while.
이 예는 유효합니다.먹어봐.
var array = [0,1,2];
for( var i = 0, ii = array.length; i < ii; i++){
if(i === 1){
break;
}
}
나는 사용할 것이다.returnbreak.
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
}
}
언급URL : https://stackoverflow.com/questions/13843972/angular-js-break-foreach
'programing' 카테고리의 다른 글
| ng-repeat을 사용한 목록 페이지 번호 지정 (0) | 2023.04.04 |
|---|---|
| 오류 응답 Spring Boot에서 "message" 필드가 비어 있습니다. (0) | 2023.04.04 |
| Objective-C 개체를 JSON으로 직렬화 및 직렬화 해제 (0) | 2023.04.04 |
| TypeScript를 사용한 개방형 함수 인수 (0) | 2023.03.25 |
| React 프로젝트의 .gitignore에 있어야 하는 파일 또는 폴더는 무엇입니까? (0) | 2023.03.25 |