Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created August 1, 2019 14:29
Show Gist options
  • Save IgorDePaula/3939f937fbdb30a1a577c90fbc7de519 to your computer and use it in GitHub Desktop.
Save IgorDePaula/3939f937fbdb30a1a577c90fbc7de519 to your computer and use it in GitHub Desktop.
reducer count names js
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
var countedNames = names.reduce(function (allNames, name) {
if (name in allNames) {
allNames[name]++;
}
else {
allNames[name] = 1;
}
return allNames;
}, {});
console.log(countedNames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment