ㅇㅇㅈ Blog

프론트엔드 수행중

0%

2022-06-14-TIL

객체 배열 true false 반환

Array.prototype.some()

객체로 이루어진 배열에서 true false 를 반환하는 메소드

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/some

미니 프로젝트를 하다가 객체로 이루어진 배열에서 true false를 반환 해야 했는데
forEach로 하나씩 비교하다 보니 사이드 이펙트가 일어나면서 버그가 생겼다..

some()을 이용하면 배열을 순환 후 조건에 알맞는 boolean을 반환 한다

사용법은 filter()와 동일 하나, filter는 새로운 배열을 반환한다는 점이 차이점

1
2
3
4
5
6
7
const array = [1, 2, 3, 4, 5];

// checks whether an element is even
const even = (element) => element % 2 === 0;

console.log(array.some(even));
// true

실제 project에서는 여러번 사용이 되어 함수로 만들어 주었다

1
2
3
4
// store에 있는 clipped배열안의 headline값과 내가 입력한 headline의 값을 비교하여 true false를 반환한다
const clipCheck = (clipped) => {
return !clipped.some((storeData) => storeData.headLine === headLine);
};