Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. var votes = [
  2. 'angular',
  3. 'angular',
  4. 'react',
  5. 'react',
  6. 'react',
  7. 'angular',
  8. 'ember',
  9. 'react',
  10. 'vanilla'
  11. ];
  12.  
  13. var initialValue = {};
  14.  
  15. var reducer = function(tally, vote) {
  16. if(!tally[vote]) {
  17. tally[vote] = 1;
  18. } else {
  19. tally[vote] = tally[vote] + 1;
  20. }
  21.  
  22. return tally;
  23. }
  24.  
  25. var result = votes.reduce(reducer, initialValue);
  26.  
  27. console.log(result);
  28. // Object {angular: 3, react: 4, ember: 1, vanilla: 1}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement