I\'m trying to iterate through an array of digits and count how many times each digit is found in the array.
In ruby it\'s easy, I just declare a Hash.new(0)
Hash.new(0)
In Javascript you do it with Array.reduce
const reducer = (acc, e) => acc.set(e, (acc.get(e) || 0) + 1); [1, 0, 0, 0, 1, 0, 0, 1].reduce(reducer, new Map()) //⇒ Map(2) {1 => 3, 0 => 5}