Array.filter()
조건을 통과한 항목만 새 배열에 모읍니다.
기본 문법
items.filter(value => condition)작은 예제 javascript
const scores = [40, 80, 65, 90];
console.log(JSON.stringify(scores.filter(score => score >= 70)));실행 결과
[80,90]
헷갈리기 쉬운 점
- 항목의 값 자체를 변환하지 않습니다. 변환에는 map을 사용합니다.
- 일치하는 항목이 없으면 undefined가 아니라 빈 배열을 반환합니다.