ES6 문법 중 map(), filter(), reduce() 의 특징을 알아보자
const lists = [one, two, three, four]
(1) Array.prototype.map()
Array 에 있는 요소들을 모두 조건에 맞게 변경하여 배열을 만들어준다.
lists.map(list => list.length);
//결과는
lists = [3, 3, 4, 4]
(2) Array.prototype.filter()
Array 에 있는 요소들을 모두 조건과 비교 후 true인 요소들만 배열로 만들어준다.
lists.map(list => list.length > 3);
//결과는
lists = [three, four]
(3) Array.prototype.reduce()
Array 에 있는 요소들을 모두 조건에 맞게 계산 한 뒤, 하나의 값으로 내보낸다.
(1) 에서 map()함수 이후 나온값을 가지고 사용
lists = [3, 3, 4, 4]
const result = lists.reduce((acc, cur, idx) => { return acc += cur; }, 0);
//결과는
result = 20