Javascript를 사용하다 보면 Array에서 필요한 요소만 추출하고 싶을 때가 있다.
그때 사용할 수 있는 것이 filter함수이다.
해당 함수의 사용법은 MDN Web Docs에서 쉽게 찾아볼 수 있다.
Array.prototype.filter()
Array 인스턴스의 filter() 메서드는 주어진 배열의 일부에 대한 얕은 복사본을 생성하고, 주어진 배열에서 제공된 함수에 의해 구현된 테스트를 통과한 요소로만 필터링 합니다.
const words = ['spray', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((word) => word.length > 6);
console.log(result);
// Expected output: Array ["exuberant", "destruction", "present"]
'FrontEnd > Javascript' 카테고리의 다른 글
javascript 프로토타입(prototype) 에 대해 이해하기 (0) | 2023.12.06 |
---|