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. C++拾遗(三)关于复合类型

    数组相关 初始化只能在定义的时候使用,不能把数组赋给另一个数组. 初始化可以提供比元素数目少的初值,其它元素将被置为0. 字符char数组只有在以\0结尾时才是一个字符串.sizeof()返回数组的长 ...

  2. 跟我学android-常用控件之EditText

    EditText 是TextView的直接子类,它与TextView的区别在于,EditText可以接受用户输入. 下面通过一个实例来说明EditText的用法 实例:sina 微博的登录界面(注意, ...

  3. 简洁的MysqlHelper

    把MySqlXXX的类更改为SqlXXX就可以成为sqlHelper. 另外C#也提供了MysqlHelper和sqlHelper,用起来也挺方便的. public class MySqlHelper ...

  4. Delphi-CompareText 函数

    函数名称 CompareText 所在单元 System.SysUtils 函数原型 function CompareText(const S1, S2: string): Integer; 函数功能 ...

  5. 一个用python实现的东方时尚(驾校)抢课程序

    2014-12-23 东方时尚约车还要网上选课,资源太紧张了,于是我决定自己写一个抢票程序来帮我刷票. 第一步,抓包.浏览器登陆选课系统,用抓包工具观察网络行为,这里我用的fildder.这里抓包主要 ...

  6. python的sorted相关

    Python 字典排序 在python里,字典是内置的数据类型,是个无序的存储结构,每一个元素是key-value对: 有关key的解释: sorted(L,key=by_name)中的key即by_ ...

  7. struct和typedef struct的用法

    我首先想到的去MSDN上看看sturct到底是什么东西,虽然平时都在用,但是每次用的时候都搞不清楚到底这两个东西有什么区别,既然微软有MSDN,我们为什么不好好利用呢,下面是摘自MSDN中的一段话: ...

  8. Mesh绘制雷达图(UGUI)

    参考资料:http://www.cnblogs.com/jeason1997/p/5130413.html ** 描述:雷达图 刷新 radarDate.SetVerticesDirty(); usi ...

  9. bing---iis how to process http request

    http://msdn.microsoft.com/en-us/library/ms524901(v=vs.90).aspx http://msdn.microsoft.com/en-us/magaz ...

  10. onCreateOptionsMenu与onCreateContextMenu差别

    onCreateOptionsMenu只会在启动时调用一次,而onCreateContextMenu则每次都会调用,这是因为onCreateContextMenu需要为所有的View控件的上下文菜单服 ...