07-10. 배열내장 함수(reduce)2
const alphabets = ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'e'];
const counts = alphabets.reduce((acc, cur) => {
if(acc[cur]) {
acc[cur] += 1;
} else {
acc[cur] = 1;
}
return acc;
}, {}) //({}은 초기 accumulator가 됩니다. 즉 초기세팅으로 객체를 만든것입니다.)
console.log(counts);// 결과)
{a: 3, b: 1, c: 2, d: 1, e: 1}
a: 3
b: 1
c: 2
d: 1
e: 1
__proto__: ObjectLast updated