HTML: Markup language
CSS: Styling language
JavaScript: Scripting language
Web APIs: Programming interfaces
All web technology
Learn web development
Discover our tools
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
View in English Always switch to English
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.
some() 메서드는 배열 안의 어떤 요소라도 주어진 판별 함수를 적어도 하나라도 통과하는지 테스트합니다. 만약 배열에서 주어진 함수가 true을 반환하면 true를 반환합니다. 그렇지 않으면 false를 반환합니다. 이 메서드는 배열을 변경하지 않습니다.
some()
const array = [1, 2, 3, 4, 5]; // Checks whether an element is even const even = (element) => element % 2 === 0; console.log(array.some(even)); // Expected output: true
// 화살표 함수 some((element) => { /* … */ }) some((element, index) => { /* … */ }) some((element, index, array) => { /* … */ }) // 콜백 함수 some(callbackFn) some(callbackFn, thisArg) // 인라인 콜백 함수 some(function (element) { /* … */ }) some(function (element, index) { /* … */ }) some(function (element, index, array) { /* … */ }) some(function (element, index, array) { /* … */ }, thisArg)
callbackFn
다음의 인자와 함께 함수를 호출합니다.
element
처리할 배열 내 현재 요소
index
처리할 현재 요소의 인덱스
array
some을 호출한 배열
some
thisArg Optional
thisArg
this
콜백 함수가 적어도 배열 중 하나의 요소에 대해 참인 값을 반환하면 true를 반환하며, 그렇지 않으면 false를 반환합니다.
true
false
some 메서드는 반복 메서드입니다. 이 메서드는 주어진 callbackFn함수가 참 같은 값을 반환할 때까지 배열 안에 있는 각각의 요소마다 한 번씩 호출합니다. 만약 그러한 요소를 찾았으면 some() 메서드는 그 즉시 true를 반환하며 배열 순회를 멈춥니다. 그렇지 않고 callbackFn이 모든 요소에 대해 거짓같은 값을 반환하면 some()은 false를 반환합니다.
some()은 수학에서 존재 한정자와 같은 역할을 합니다. 특히 빈 배열의 경우 모든 조건에 대해 false를 반환합니다.
callbackFn는 값이 할당된 배열 인덱스에서만 실행됩니다. 희소 배열의 빈 슬롯에서는 실행되지 않습니다.
some()은 호출된 배열의 값을 변경하지 않지만, 제공된 callbackFn에서는 가능합니다. 그럼에도 배열의 길이는 callbackFn의 첫 실행 이전에 저정된다는 점을 명심하시기 바랍니다.
some() 메서드는 제너릭입니다. this 값에 length 속성과 정수 키 속성만 있으면 됩니다.
length
다음 예제는 배열 내 요소 중 하나라도 10보다 큰지 판별합니다.
function isBiggerThan10(element, index, array) { return element > 10; } [2, 5, 8, 1, 4].some(isBiggerThan10); // false [12, 5, 8, 1, 4].some(isBiggerThan10); // true
화살표 함수는 같은 테스트에 대해 더 짧은 구문을 제공합니다.
[2, 5, 8, 1, 4].some((x) => x > 10); // false [12, 5, 8, 1, 4].some((x) => x > 10); // true
includes() 메서드의 기능을 모방하기 위해 이 사용자 지정 함수는 요소가 배열에 있는 경우 true 반환합니다.
includes()
const fruits = ["apple", "banana", "mango", "guava"]; function checkAvailability(arr, val) { return arr.some((arrVal) => val === arrVal); } checkAvailability(fruits, "kela"); // false checkAvailability(fruits, "banana"); // true
const TRUTHY_VALUES = [true, "true", 1]; function getBoolean(value) { if (typeof value === "string") { value = value.toLowerCase().trim(); } return TRUTHY_VALUES.some((t) => t === value); } getBoolean(false); // false getBoolean("false"); // false getBoolean(1); // true getBoolean("true"); // true
some()은 빈 슬롯에 조건자를 실행하지 않습니다.
console.log([1, , 3].some((x) => x === undefined)); // false console.log([1, , 1].some((x) => x !== 1)); // false console.log([1, undefined, 1].some((x) => x !== 1)); // true
some() 메서드는 this의 length 속성을 읽고 객체의 마지막에 도달하거나 callbackFn이 true를 반환할 때 까지 정수 인덱스로 접근합니다.
const arrayLike = { length: 3, 0: "a", 1: "b", 2: "c", }; console.log(Array.prototype.some.call(arrayLike, (x) => typeof x === "number")); // false
Enable JavaScript to view this browser compatibility table.
Array.prototype.some
core-js
Array.prototype.every()
Array.prototype.forEach()
Array.prototype.find()
TypedArray.prototype.some()