I want to count the number of occurrences of each character in a given string using JavaScript.
For example:
var str = \"I want to count the number
Shorter answer, with reduce:
let s = 'hello'; var result = [...s].reduce((a, e) => { a[e] = a[e] ? a[e] + 1 : 1; return a }, {}); console.log(result); // {h: 1, e: 1, l: 2, o: 1}