[Javascript] Array.prototype.some() 메서드

Yuri Lee·2022년 2월 21일
0

Intro

처음보는 자바스크립트 메서드를 발견했다. 😣 😣 자바스크립트 너어... 까면 깔수록 새로운 면모들이 계속 나오잖아...?

Array.prototype.some()

some() 메서드는 배열 안의 어떤 요소라도 주어진 판별 함수를 통과하는지 테스트한다.

Syntax

arr.some(callback[, thisArg])

parameter

  • callback: 각 요소를 시험할 함수. 다음 세 가지 인수를 받는다.
  • currentValue :처리할 현재 요소.
  • index [Optional] : 처리할 현재 요소의 인덱스.
  • array [Optional] : some을 호출한 배열.
  • thisArg [Optional] : callback을 실행할 때 this로 사용하는 값.

return value

callback이 어떤 배열 요소라도 대해 참인(truthy) 값을 반환하는 경우 true, 그 외엔 false.

Example

function isBiggerThan10(element, index, array) {
  return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
var fruits = ['apple', 'banana', 'mango', 'guava'];

function checkAvailability(arr, val) {
    return arr.some(arrVal => val === arrVal);
}

checkAvailability(fruits, 'kela'); //false
checkAvailability(fruits, 'banana'); //true

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/some#%EA%B5%AC%EB%AC%B8
https://mine-it-record.tistory.com/377

profile
Step by step goes a long way ✨

0개의 댓글