Skip to content

Instantly share code, notes, and snippets.

@yaminmhd
Created March 25, 2018 05:23
Show Gist options
  • Select an option

  • Save yaminmhd/56873135c33c6df7dbb788dd0418da7a to your computer and use it in GitHub Desktop.

Select an option

Save yaminmhd/56873135c33c6df7dbb788dd0418da7a to your computer and use it in GitHub Desktop.
high order function reducer
const votes = [
'tacos',
'pizza',
'pizza',
'tacos',
'fries',
'ice cream',
'ice cream',
'pizza'
]
const result = votes.reduce((tally,vote) => {
if(!tally[vote]){
tally[vote] = 1
}else{
tally[vote] += 1
}
return tally;
}, {})
console.log(result) // {tacos: 2, pizza: 3, fries: 1, ice cream: 2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment