码迷,mamicode.com
首页 > 编程语言 > 详细

[Javascript] Advanced Reduce: Common Mistakes

时间:2015-12-01 07:05:11      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

Take away:

  • Always check you ruturn the accumulator
  • Always pass in the inital value

 

var data = ["vote1", "vote2", "vote1", "vote2"];

var reducer = function(acc, value){
   if(acc[value]){
     acc[value] = acc[value] + 1;
   }else{
     acc[value] = 1;
   }
  
  return acc;
};

var res = data.reduce(reducer ,{});

console.log(res);

 

[Javascript] Advanced Reduce: Common Mistakes

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5008972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!