zoukankan      html  css  js  c++  java
  • js的reduce方法

    reduce()方法接受一个函数进行累加计算(reduce()对于空数组是不会执行回调函数的)

    使用语法:

    array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

    total:初始值,或者计算结束后返回的返回值(必需)

    currentValue:当前元素(必需)

    currentIndex:当前元素的索引

    arr:当前元素所属的数组对象

    假如在reduce的回调函数里面没有返回值时

    var arr = [5,6,7,8,9]
    arr.reduce(function(total, currentValue, currentIndex, arr){
      console.log('reduce:', total, currentValue, currentIndex, arr)
    })

    打印出来的结果如下

    reduce: 5 6 1 [ 5, 6, 7, 8, 9 ]
    reduce: undefined 7 2 [ 5, 6, 7, 8, 9 ]
    reduce: undefined 8 3 [ 5, 6, 7, 8, 9 ]
    reduce: undefined 9 4 [ 5, 6, 7, 8, 9 ]

    由此分析:

    当reduce循环时,total的初始值是数组第一位的值,由于没有return的值,所以后面都是undefined,

  • 相关阅读:
    浮动
    导航
    Json
    节点
    评论框
    WebClient 指定出口 IP
    IIS8 下 JS, CSS 等静态文件出现 500 错误
    使用 ffmpeg 转换 mov 视频
    使用 ildasm 和 ilasm 修改程序集的的引用信息
    2020-01-08 工作日记:无题
  • 原文地址:https://www.cnblogs.com/wangxirui/p/11993977.html
Copyright © 2011-2022 走看看