Array Method 01. filter()

주히 🌼·2021년 1월 31일
0

Array Method

목록 보기
1/2

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로 사용하는 값

반환 값

테스트를 통과한 요소로 이루어진 새로운 배열로, 어떤 요소도 테스트를 통과하지 못할 경우 빈 배열을 반환한다.

profile
하면 된다! 프론트앤드 공부 중 입니당 🙆‍♀️

0개의 댓글