Array.prototype.every()
every()
메서드는 배열 안의 모든 요소가 주어진 판별 함수를 통과하는지 테스트합니다.
참고: 빈 배열에서 호출하면 무조건 true
를 반환합니다.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
구문
arr.every(callback[, thisArg])
매개변수
callback
- 각 요소를 시험할 함수. 다음 세 가지 인수를 받습니다.
currentValue
- 처리할 현재 요소.
index
Optional- 처리할 현재 요소의 인덱스.
array
Optional- every를 호출한 배열.
thisArg
Optionalcallback
을 실행할 때this
로 사용하는 값.
반환 값
callback
이 모든 배열 요소에 대해 참(truthy)인 값을 반환하는 경우 true
, 그 외엔 false
.
설명
every
는 callback
이 거짓을 반환하는 요소를 찾을 때까지 배열에 있는 각 요소에 대해 한 번씩 callback
함수를 실행합니다. 해당하는 요소를 발견한 경우 every
는 즉시 false
를 반환합니다. 그렇지 않으면, 즉 모든 값에서 참을 반환하면 true를 반환합니다. 할당한 값이 있는 배열의 인덱스에서만 callback
을 호출합니다. 삭제했거나 값을 할당한 적이 없는 인덱스에서는 호출하지 않습니다.
callback
은 요소의 값, 해당 요소의 인덱스 및 순회하고 있는 배열 세 가지 인수와 함께 호출됩니다.
thisArg
매개변수를 every에 제공한 경우 callback
의 this
로 사용됩니다. 그 외엔 undefined
값을 사용합니다. 최종적으로 callback
이 볼 수 있는 this
의 값은 함수가 볼 수 있는 this
를 결정하는 평소 규칙을 따릅니다.
every
는 호출한 배열을 변형하지 않습니다.
every
가 처리하는 요소의 범위는 callback
의 첫 호출 전에 설정됩니다. every
호출 이후로 배열에 추가하는 요소는 callback
이 방문하지 않습니다. 배열에 원래 있었지만 아직 방문하지 않은 요소가 callback
에 의해 변경된 경우, 그 인덱스를 방문하는 시점의 값을 사용합니다. 삭제한 요소는 방문하지 않습니다.
every
는 (이산)수학에서 전칭(∀) 정량자quantifier(한정자)처럼 행동합니다. 특히, 빈 배열에 대해서는 true
를 반환합니다. (이는 공집합의 모든 요소가 어떠한 주어진 조건도 만족하는 공허한 참입니다.)
예제
모든 배열 요소의 크기 테스트
다음 예는 배열의 모든 요소가 10보다 더 큰지 테스트합니다.
function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
화살표 함수 사용
화살표 함수는 같은 테스트에 대해 더 짧은 구문을 제공합니다.
[12, 5, 8, 130, 44].every(elem => elem >= 10); // false
[12, 54, 18, 130, 44].every(elem => elem >= 10); // true
폴리필
every
는 ECMA-262 표준 제5판에 추가됐으므로 어떤 표준 구현체에서는 사용하지 못할 수도 있습니다. 다른 모든 코드 이전에 아래 코드를 포함하면 every
를 지원하지 않는 환경에서도 사용할 수 있습니다. 아래 알고리즘은 Object
와 TypeError
가 변형되지 않고, callbackfn.call
의 계산 값이 원래의 Function.prototype.call()
과 같은 경우 ECMA-262 제5판이 명시한 것과 동일합니다.
if (!Array.prototype.every) {
Array.prototype.every = function(callbackfn, thisArg) {
'use strict';
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the this
// value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method
// of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (typeof callbackfn !== 'function') {
throw new TypeError();
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0.
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal
// method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
// i. Let kValue be the result of calling the Get internal method
// of O with argument Pk.
kValue = O[k];
// ii. Let testResult be the result of calling the Call internal method
// of callbackfn with T as the this value and argument list
// containing kValue, k, and O.
var testResult = callbackfn.call(T, kValue, k, O);
// iii. If ToBoolean(testResult) is false, return false.
if (!testResult) {
return false;
}
}
k++;
}
return true;
};
}
명세
명세 | 상태 | 설명 |
---|---|---|
ECMAScript 5.1 (ECMA-262) The definition of 'Array.prototype.every' in that specification. |
Standard | 초기 정의. JavaScript 1.6에서 구현됨. |
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.every' in that specification. |
Standard | |
ECMAScript (ECMA-262) The definition of 'Array.prototype.every' in that specification. |
Living Standard |
브라우저 호환성
BCD tables only load in the browser