filter() 메서드는 주어진 함수의 테스트를 통과하는 모든 요소를 모아 새로운 배열로 반환한다.
💻 C O D E 💻
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const result = words.filter(word => word.length > 6); console.log(result); // expected output: Array ["exuberant", "destruction", "present"]
arr.filter(callback(element[, index[, array]])[, thisArg])
callback : 각 요소를 시험할 함수로, true를 반환하면 요소를 유지하고, false를 반환하면 버린다.
아래와 같은 세 가지 매개변수를 받는다.
① element : 처리할 현재 요소
② index : 처리할 현재 요소의 인덱스
③ array : filter를 호출할 배열
thisArg : callback 을 실행할 때 this로 사용하는 값
테스트를 통과한 요소로 이루어진 새로운 배열로, 어떤 요소도 테스트를 통과하지 못할 경우 빈 배열을 반환한다.