[TIL] D+60 고차함수 기초 개념 공부

밍징·2021년 7월 29일
0

TIL_ver.

목록 보기
12/49
post-thumbnail

📌 오늘 공부한 것

  • 고차함수 map()메서드 개념
    map() 메서드는 배열 내의 모든 요소 각각에 대하여 주어진 함수를 호출한 결과를 모아 새로운 배열을 반환
예시)
const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]
  • 고차함수 filter()메서드 개념
    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"]
  • 고차함수 reduce()메서드 개념
    reduce() 메서드는 배열의 각 요소에 대해 주어진 리듀서(reducer) 함수를 실행하고, 하나의 결과값을 반환
예시)
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
  • 코플릿 고차함수 1번~10번까지 풀기
profile
프론트엔드를 공부하고 있는 디자이너 입니다 :D

0개의 댓글