배열의 각 요소에 대해 주어진 리듀서(reducer)함수를 콜백함수로 실행하고 하나의 결과값을 반환
1) reduce()는 2개의 인자값을 받는다
2) reducer 함수는 4개의 인자값을 받는다
3) 사용예시
a. 배열에서의 덧셈 (초기값 없을때 / 있을때)
const arr = [1, 2, 3, 4, 5];
console.log("초기값 없음");
arr.reduce((total, now, nowIdx, arr) => {
console.log("total :", total);
console.log("now :", now);
console.log("nowIdx :", nowIdx);
console.log("arr :", arr);
console.log("\n");
return total + now;
});

console.log("\n초기값 있음");
arr.reduce((total, now, nowIdx, arr) => {
console.log("total :", total);
console.log("now :", now);
console.log("nowIdx :", nowIdx);
console.log("arr :", arr);
console.log("\n");
return total + now;
}, 0);

b. object 배열에서의 활용 -그룹핑
//그룹핑
let people = [
{ name: 'Alice', age: 21 },
{ name: 'Max', age: 20 },
{ name: 'Jane', age: 20 }
];
function groupBy(objectArray, property) {
return objectArray.reduce(function (acc, obj) {
let key = obj[property]
if (!acc[key]) {
acc[key] = []
}
acc[key].push(obj)
return acc
}, {})
}
let groupedPeople = groupBy(people, 'age')
출처: https://jo-c.tistory.com/54 [조씨의 개발 블로그:티스토리]

c. object 배열에서의 활용 - 카운팅
//카운팅
let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
let countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++
}
else {
allNames[name] = 1
}
return allNames
}, {})
출처: https://jo-c.tistory.com/54 [조씨의 개발 블로그:티스토리]
