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. [转帖]了解AmbiLight知识

    了解科技前沿的方法..American Online=AOL.algorithm算法.(Denzel say, and I don't know) Engadget瘾科技网站.英文版Engadget网 ...

  2. strace 使用

  3. [Head First设计模式笔记]----命令模式

    命令模式定义:将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象.命令模式也支持可撤销的操作. 类图: 适用设计方案举例:实现一种遥控器,该遥控器具有七个可编程的插槽(每个都可以指 ...

  4. Semaphore (通常用于限制可以访问某些资源(物理或逻辑的)的线程数目)

    Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目.例如,下面的类使用信号量控制对内容池的访问: 方法详解: 构造方法摘要 Semaphore(int permits)     ...

  5. Selenium 下载URL

    http://mvnrepository.com/artifact/org.seleniumhq.selenium

  6. 关于jQuery表单校验的应用

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. js循环便利json数据

    var data=[{name:"a",age:12},{name:"b",age:11},{name:"c",age:13},{name: ...

  8. C语言基础学习运算符-关系运算符

    比较大小 首先,我们得先了解一下布尔类型.C语言的C99标准支持布尔类型,关键字为_Bool,用于表示逻辑值true与false.C语言用值1表示true, 用值0表示false.因此布尔类型实际上是 ...

  9. centos 安装 mongo3.0

    官方网站传贴,每次去翻doc,麻烦 vi  /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0]name=MongoDB Repository ...

  10. 【转】Windows平台下Git服务器搭建

    Windows平台下Git服务器搭建 Posted on 2015-05-18 21:29 阿祥当码农 阅读(7637) 评论(0) 编辑 收藏 该文章转自:http://www.codeceo.co ...