목록맵 (2)
taenyLog
map 메서드와 filter 메서드의 공통점과 차이점을 알아보자. 우선 map()는 배열의 각 요소에 대해 주어진 함수를 호출하고, 그 결과를 새로운 배열로 반환한다. 원본은 변경되지 않고 새로운 배열이 생성된다. 각 요소를 변환하고, 변환된 값을 가진 새로운 배열을 반환한다. map()의 예시 const numbers = [1, 2, 3, 4, 5]; const squaredNumbers = numbers.map(x => x * x); // squaredNumbers는 [1, 4, 9, 16, 25]가 됩니다. map()은 언제 사용하지 ? - 변환 작업 : 각 요소를 가공하거나 변환하여 새로운 값의 배열을 생성 - 데이터 가공 : 데이터를 가져와서 필요한 형식으로 가공하거나, 특정 속성을 추출하여 새..
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"] 원본 ..