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的更多相关文章

  1. [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight

    Learn a few advanced reduction patterns: flatten allows you to merge a set of arrays into a single a ...

  2. [Javascript] Introducing Reduce: Common Patterns

    Learn how two common array functions - map() and filter() - are syntactic sugar for reduce operation ...

  3. [Javascript] Advanced Reduce: Additional Reducer Arguments

    Sometimes we need to turn arrays into new values in ways that can't be done purely by passing an acc ...

  4. [Javascript] Advanced Reduce: Composing Functions with Reduce

    Learn how to use array reduction to create functional pipelines by composing arrays of functions. co ...

  5. [转]50 Shades of Go: Traps, Gotchas, and Common Mistakes for New Golang Devs

    http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/ 50 Shades of Go: Traps, Gotc ...

  6. Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference

    (Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...

  7. JavaScript中reduce()方法

    原文  http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/   JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...

  8. Nginx:Pitfalls and Common Mistakes

    New and old users alike can run into a pitfall. Below we outline issues that we see frequently as we ...

  9. JavaScript: Advanced

    DOM 1. 节点 getElementsByName方法 <!DOCTYPE HTML> <html> <head> <script type=" ...

随机推荐

  1. ORACLE 解析xml字符串-转载的

    --------------------------方法一------------------------------------- 1.xml字符串 /* <orderlist>     ...

  2. UITableView中的visibleCells的用法(visibleCells帮上大忙了)

      这两天遇到一个问题,UITableView中需要加入动画,而且每一行的速度不一样. 刚开始做时把所有的cell都遍历一遍加上动画,后来发现,如果数据很多时,就会出现各种各样的问题,而且没有显示在界 ...

  3. iOS 格式化输出符号与类型转换

    1.iOS 格式化输出符号 %@    对象 %d,   %i 整数 %u     无符号整形 %f      浮点(双字节) %x,   %X  二进制整数 %o     八进制整数 %zi     ...

  4. OC中的单例设计模式及单例的宏抽取

    // 在一个对象需要重复使用,并且很频繁时,可以对对象使用单例设计模式 // 单例的设计其实就是多alloc内部的allocWithZone下手,重写该方法 #pragma Person.h文件 #i ...

  5. Taxi Cab Scheme POJ && HDU

    Online Judge Problem Set Authors Online Contests User Web Board Home Page F.A.Qs Statistical Charts ...

  6. Vim简明教程【CoolShell】(转)

    vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...

  7. vim插件和配置

    vim插件和配置 插件 pathogen 可以方便地管理vim插件 在没有pathogen的情况下,vim插件的文件全部都放在.vim目录,卸载插件很麻烦,pathogen可以将不同的插件放在一个单独 ...

  8. PHP自定义错误处理

    自定义错误报告的处理方式,可以完全绕过标准的PHP错误处理函数,这样就可以按照自己定义的格式打印错误报告,或改变错误报告打印的位置(标准PHP的错误报告是哪里发生错误就在发生位置处显示).以下几种情况 ...

  9. spring的beans.xml的配置

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. bzoj1188: [HNOI2007]分裂游戏

    Description 聪聪和睿睿最近迷上了一款叫做分裂的游戏. 该游戏的规则试: 共有 n 个瓶子, 标号为 0,1,2.....n-1, 第 i 个瓶子中装有 p[i]颗巧克力豆,两个人轮流取豆子 ...