JavaScript 배열 원소별 개수 세기

1 개요[ | ]

JavaScript Array - Counting the occurrences
JavaScript 배열 값 개수 세기
JavaScript 배열 원소별 개수 세기
const arr = ['b', 'x', 'b', 'o', 'b', 'o', 'b'];
const occurrences = {};
for (const v of arr) {
  occurrences[v] = occurrences[v] ? occurrences[v] + 1 : 1;
}
console.log(occurrences); // {b: 4, x: 1, o: 2}
const arr = ['b', 'x', 'b', 'o', 'b', 'o', 'b'];
const occurrences = arr.reduce((a, i) => {return a[i]=(a[i]||0)+1, a},{});
console.log(occurrences); // {b: 4, x: 1, o: 2}

2 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}