peterschussheim
10/13/2016 - 4:36 PM

reduce2

reduce2

var data = ["vote1", "vote2", "vote1", "vote2"];
function reducer(accumulator, value) {
  if (accumulator[value]) {
    accumulator[value] = accumulator[value] + 1;
  } else {
    accumulator[value] = 1;
  }

  return accumulator; ///ALWAYS remember to return the accumulator.
}

var tally = data.reduce(reducer, []); ///Always pass in an initial value for an accumulator.

reduce2

This Gist was automatically created by Carbide, a free online programming environment.

You can view a live, interactive version of this Gist here.