JavaScript for 루프를 중지하는 방법은 무엇입니까?
이 JavaScript를 사용하여 배열을 반복하고 일치하는 배열 요소를 찾습니다.
var remSize = [],
szString, remData, remIndex, i;
for (i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
remSize[i].size == remData.size ? remIndex = i : remIndex = -1;
}
배열에는 다음 "크기"가 포함 ["34", "36", "38"...]
됩니다..
remData.size
내가 찾고있는 "크기"입니다 (예 : "36").
i
검색중인 크기 가 인덱스에 있으면 인덱스를 반환해야합니다 . 그렇지 않으면 반환해야합니다 -1
. 이 작업을 수행하는 더 좋은 방법이 있습니까?
for
JavaScript에서 초기에 루프 를 중지하려면 다음을 사용합니다 break
.
var remSize = [],
szString,
remData,
remIndex,
i;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // Set a default if we don't find it
for (i = 0; i < remSize.length; i++) {
// I'm looking for the index i, when the condition is true
if (remSize[i].size === remData.size) {
remIndex = i;
break; // <=== breaks out of the loop early
}
}
ES2015 (일명 ES6) 환경에있는 경우이 특정 사용 사례의 경우 Array#findIndex
(항목의 인덱스 Array#find
를 찾기 위해 ) 또는 (항목 자체를 찾기 위해) 사용할 수 있으며, 둘 다 shimm / polyfilled 할 수 있습니다.
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = remSize.findIndex(function(entry) {
return entry.size === remData.size;
});
Array#find
:
var remSize = [],
szString,
remData,
remEntry;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remEntry = remSize.find(function(entry) {
return entry.size === remData.size;
});
Array#findIndex
stops the first time the callback returns a truthy value, returning the index for that call to the callback; it returns -1
if the callback never returns a truthy value. Array#find
also stops when it finds what you're looking for, but it returns the entry, not its index (or undefined
if the callback never returns a truthy value).
If you're using an ES5-compatible environment (or an ES5 shim), you can use the new some
function on arrays, which calls a callback until the callback returns a truthy value:
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // <== Set a default if we don't find it
remSize.some(function(entry, index) {
if (entry.size === remData.size) {
remIndex = index;
return true; // <== Equivalent of break for `Array#some`
}
});
If you're using jQuery, you can use jQuery.each
to loop through an array; that would look like this:
var remSize = [],
szString,
remData,
remIndex;
/* ...I assume there's code here putting entries in `remSize` and assigning something to `remData`... */
remIndex = -1; // <== Set a default if we don't find it
jQuery.each(remSize, function(index, entry) {
if (entry.size === remData.size) {
remIndex = index;
return false; // <== Equivalent of break for jQuery.each
}
});
The logic is incorrect. It would always return the result of last element in the array.
remIndex = -1;
for (i = 0; i < remSize.length; i++) {
if (remSize[i].size == remData.size) {
remIndex = i
break;
}
}
Use for of loop instead which is part of ES2015 release. Unlike forEach, we can use return, break and continue. See https://hacks.mozilla.org/2015/04/es6-in-depth-iterators-and-the-for-of-loop/
let arr = [1,2,3,4,5];
for (let ele of arr) {
if (ele > 3) break;
console.log(ele);
}
참고URL : https://stackoverflow.com/questions/9830650/how-to-stop-a-javascript-for-loop
'IT story' 카테고리의 다른 글
JAXBElement를 인스턴스화하는 방법 (0) | 2020.08.08 |
---|---|
HTML 캔버스 JavaScript 라이브러리 및 프레임 워크의 최신 기술은 무엇입니까? (0) | 2020.08.08 |
GitHub에서 리포지토리 프로젝트의 생성 날짜를 찾는 방법은 무엇입니까? (0) | 2020.08.08 |
C에서 #pragma 사용 (0) | 2020.08.08 |
동일한 이름의 모듈이있는 경우 내장 라이브러리에서 가져 오기 (0) | 2020.08.08 |