반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

taenyLog

[JavaScript] Filter Method 본문

Web

[JavaScript] Filter Method

taenyLog 2023. 9. 22. 11:49
반응형

Filter Method  필터 메서드

create a new array with all elements that pass the test implemented by the provided function

Filter는 요소로 구성된 배열에서 필터링을 하거나 부분 집합을 모아 새 배열을 만드는 데 쓰임.

 

 

 

 

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"]

원본 배열을 바꾸지 않는다.

 

 

 

 

평점이 가장 높은 아이템, 오래된 아이템, 뉴아이템 등에 적용할 수 있다. 

평론가들의 호평을 받은 영화 등 

const movies =  [ 
    {title:'a', score : 99, year:1984},
    {title:'b', score : 49, year:2004}, ....... ]



const goodMovies = movies.filter(m => m.score > 80)
const goodTitles = goodMovies.map(m => m.title)
// 위의 두 문장을 아래의 한문장으로 바꿔서 쓸 수 있다.
movies.filter(m => m.score > 80).map(m => m.title)



const badMovies = movies.filter(m => m.score < 80)
const recentMovies = movies.filter(m => m.year >2000)

 

 

 

반응형