HTML&CSS&Javascript/JS

[JS] every(), some() 함수 정리

킹우현 2023. 8. 18. 23:51

Array.prototype.every()

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

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

console.log(array1.every(isBelowThreshold));
// Expected output: true

 

every() 메서드는 대상 배열안의 "모든" 요소가 특정 조건에 만족하는지 확인하는 메서드이다.

 

Array.prototype.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() 메서드는 대상 배열에 특정 조건에 만족하는게 "단 하나"라도 존재하는지 확인하는 메서드이다.