Sometimes we need to turn arrays into new values in ways that can't be done purely by passing an accumulator along with no knowledge about its context. Learn how to reduce an array of numbers into its mathematical mean in a single reduce step by using the optional index and array reducer arguments.

function reducer(accumulator, value, index, array) {
var intermediaryValue = accumulator + value; if (index === array.length - 1) {
return intermediaryValue / array.length;
} return intermediaryValue;
} var data = [1, 2, 3, 3, 4, 5, 3, 1];
var mean = data.reduce(reducer, 0); console.log(mean);

[Javascript] Advanced Reduce: Additional Reducer Arguments的更多相关文章

  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] Advanced Reduce: Common Mistakes

    Take away: Always check you ruturn the accumulator Always pass in the inital value var data = [" ...

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

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

  4. JavaScript中reduce()方法

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

  5. javascript 函数详解2 -- arguments

    今天我们接着上篇文章来继续javascript函数这个主题.今天要讲的是函数对像中一个很重要的属性--arguments. 相关阅读: javascript 函数详解1 -- 概述 javascrip ...

  6. [Javascript] Advanced Console Log Arguments

    Get more mileage from your console output by going beyond mere string logging - log entire introspec ...

  7. javascript的Function 和其 Arguments

    http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...

  8. 随笔:JavaScript函数中的对象----arguments

    关于arguments 调用函数时,如果需要传参,其实参数就是一个数组,在函数体的内置对象arguments可以访问这个数组,如: arguments[0]:第一个参数 arguments[1]:第二 ...

  9. JavaScript 没有函数重载&Arguments对象

    对于学过Java的人来说.函数重载并非一个陌生的概念,可是javaScript中有函数重载么...接下来我们就进行測试 <script type="text/javascript&qu ...

随机推荐

  1. 第一章 Javascript基础

    一.Javascript概述(知道) a.一种基于对象和事件驱动的脚本语言 b.作用: 给页面添加动态效果 c.历史: 原名叫做livescript.W3c组织开发的标准叫ECMAscipt. d.特 ...

  2. json+一般处理程序读取数据库数据

    一般处理程序的语法结构 string jsoncallback = context.Request["jsoncallback"]; 声明变量 前台传值使用        stri ...

  3. 2.常用快捷键.md

    [toc] 1.mian函数补全 在IntelJ中和Eclipse中稍有不同,在Eclipse中,输入main再按Alt+/即可自动补全main函数,但是在IntellJ中则是输入psvm,选中即可 ...

  4. Specified VM install not found: type Standard VM, name jdk1.6.0_05

    重装系统换了jdk,之前jdk用的1.6,现在改成1.7了.但是更新之后发现ant打包用不了了,报错 Specified VM install not found: type Standard VM, ...

  5. Windows Phone获得IsolatedStorage中指定目录下的所有文件

    在Windows Phone 中对隔离存储空间中的文件操作需要通过System.Io.IsolatedStorage下的类进行操作 获得指定文件夹下的所有文件: 参数:是指定文件夹的路径加上通配符,格 ...

  6. Android源码下载方法详解

    转自:http://www.cnblogs.com/anakin/archive/2011/12/20/2295276.html Android源码下载方法详解 相信很多下载过内核的人都对这个很熟悉 ...

  7. jQuery解析JSON的问题

    在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. JS ...

  8. jquery ajax (2)实例 .GET

    1js 代码 $(function(){ $("#send").click(function(){ $.get("get3.php", { username : ...

  9. 使用Intellij IDEA构建spark开发环境

    近期开始研究学习spark,开发环境有多种,由于习惯使用STS的maven项目,但是按照许多资料的方法尝试以后并没有成功,也可能是我环境问题:也可以是用scala中自带的eclipse,但是不太习惯, ...

  10. java的占位符

    java占位符的类型: 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.fo ...