JavaScript every(), some()란?

김진원·2022년 12월 18일

JS

목록 보기
7/11
post-thumbnail

every(), some()

유사한 성격의 두 함수는,
배열 내 요소들을 판별하여 결과 값을 bloolean형식으로 출력한다.

every()

arr.every(function(currentValue, index, array), thisValue))

callbackFn
각 요소를 시험할 함수. 다음 세 가지 인수를 받는다.

  • element
    배열에서 처리되는 현재 요소

  • index
    처리할 현재 요소의 인덱스

  • array
    every를 호출한 배열

  • thisArg Optional
    callbackFn을 실행할 때 this로 사용하는 값.

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold)); // true

위 예시처럼 every() 메서드는 배열 내 모든 요소가 true 일 경우 true를 반환한다.

배열 내 요소 중 하나라도 false 일 시 false를 반환한다.

some()

arr.some(function(currentValue, index, array), thisValue))

callback
각 요소를 시험할 함수. 다음 세 가지 인수를 받는다.
currentValue : 처리할 현재 요소.

  • index Optional
    처리할 현재 요소의 인덱스.

  • array Optional
    some을 호출한 배열.

  • thisArg Optional
    callback을 실행할 때 this로 사용하는 값.

function isBiggerThan10(element, index, array) {
  return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

위 예시처럼 some() 메서드는 배열 내 모든 요소가 false 일 경우 false를 반환한다.

배열 내 요소가 하나라도 true일 시 true 반환.

profile
사용자의 관점에 대해 욕심이 많은 개발자

0개의 댓글