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. 通知(Notification)的使用

    新建一个 NotificationTest项目,并修改 activity_main.xml 中的代码,如下所示:<LinearLayout xmlns:android="http:// ...

  2. jsp中文乱码终极解决方法(转)

    一, 找出问题的根源乱码可能出现的地方: jsp页面中 jsp页面之间相互传参的参数 与数据库中数据的存取 基本归纳为以上几种. 二, 寻找解决方案 出现在jsp页面中,是由于没有设置jsp页面的中文 ...

  3. GET or POST

    w3school中是这么说的: 与 POST 相比,GET 更简单也更快,并且在大部分情况下都能用. 然而,在以下情况中,请使用 POST 请求: 无法使用缓存文件(更新服务器上的文件或数据库) 向服 ...

  4. SVN的使用(转发)

    http://my.oschina.net/joanfen/blog/194491?fromerr=LM5QY3YF

  5. Cocos2dx 3.2 节点之间相互通信与设置触摸吞噬的方法

    实际开发中,我们经常会遇到这样的情况.我们有一个层layer1,这个层包含一个menu层,menu1层里又包含了一个节点按钮button1.现在需要实现一个效果:点击button1弹出一个对话框,这个 ...

  6. Unable to locate package错误解决办法

    新装了VMWare Player,结果装上Ubuntu12.04后安装软件都提示:Unable to locate package错误,解决方法非常简单,终端输入以下命令即可: sudo apt-ge ...

  7. javascript知识图谱

  8. 织梦安装过后出现"...www/include/templets/default/index.htm Not Found!"

    在织梦网站搬家之后再整站更新,往往会遇到访问首页的时候出现www/include/templets/default/index.htm Not Found!,这个问题我遇到过两次,都是这样解决的: 进 ...

  9. ggts下载地址

    地址:http://spring.io/tools/ggts See All Versions可以下载更多版本,里面包含安装版和解压版

  10. hdu2524 (求矩形个数,水题。。。)

    hdu 2524 N - 暴力求解.打表 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...