reduce 方法 (Array) (JavaScript)
对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
array1.reduce(callbackfn[, initialValue])
|
参数 |
定义 |
|---|---|
|
array1 |
必需。一个数组对象。 |
|
callbackfn |
必需。一个接受最多四个参数的函数。对于数组中的每个元素,reduce 方法都会调用 callbackfn 函数一次。 |
|
initialValue |
可选。如果指定 initialValue,则它将用作初始值来启动累积。第一次调用 callbackfn 函数会将此值作为参数而非数组值提供。 |
通过最后一次调用回调函数获得的累积结果。
当满足下列任一条件时,将引发 TypeError 异常:
callbackfn 参数不是函数对象。
数组不包含元素,且未提供 initialValue。
| Exception | Condition |
|---|
如果提供了 initialValue,则 reduce 方法会对数组中的每个元素调用一次 callbackfn 函数(按升序索引顺序)。如果未提供 initialValue,则reduce 方法会对从第二个元素开始的每个元素调用 callbackfn 函数。
回调函数的返回值在下一次调用回调函数时作为 previousValue 参数提供。最后一次调用回调函数获得的返回值为 reduce 方法的返回值。
不为数组中缺少的元素调用该回调函数。
注意 |
|---|
|
reduceRight 方法 (Array) (JavaScript)按降序索引顺序处理元素。 |
回调函数的语法如下所示:
function callbackfn(previousValue, currentValue, currentIndex, array1)
可使用最多四个参数来声明回调函数。
下表列出了回调函数参数。
|
回调参数 |
定义 |
|---|---|
|
previousValue |
通过上一次调用回调函数获得的值。如果向 reduce 方法提供 initialValue,则在首次调用函数时,previousValue 为initialValue。 |
|
currentValue |
当前数组元素的值。 |
|
currentIndex |
当前数组元素的数字索引。 |
|
array1 |
包含该元素的数组对象。 |
在第一次调用回调函数时,作为参数提供的值取决于 reduce 方法是否具有 initialValue 参数。
如果向 reduce 方法提供 initialValue:
previousValue 参数为 initialValue。
currentValue 参数是数组中的第一个元素的值。
如果未提供 initialValue:
previousValue 参数是数组中的第一个元素的值。
currentValue 参数是数组中的第二个元素的值。
数组对象可由回调函数修改。
下表描述了在 reduce 方法启动后修改数组对象所获得的结果。
|
reduce 方法启动后的条件 |
元素是否传递给回调函数 |
|---|---|
|
在数组的原始长度之外添加元素。 |
否。 |
|
添加元素以填充数组中缺少的元素。 |
是,如果该索引尚未传递给回调函数。 |
|
元素被更改。 |
是,如果该元素尚未传递给回调函数。 |
|
从数组中删除元素。 |
否,除非该元素已传递给回调函数。 |
下面的示例将数组值连接成字符串,各个值用“::”分隔开。由于未向 reduce 方法提供初始值,第一次调用回调函数时会将“abc”作为 previousValue 参数并将“def”作为 currentValue 参数。
// Define the callback function.
function appendCurrent (previousValue, currentValue) {
return previousValue + "::" + currentValue;
} // Create an array.
var elements = ["abc", "def", 123, 456]; // Call the reduce method, which calls the callback function
// for each array element.
var result = elements.reduce(appendCurrent); document.write(result); // Output:
// abc::def::123::456
下面的示例向数组添加舍入后的值。使用初始值 0 调用 reduce 方法。
// Define the callback function.
function addRounded (previousValue, currentValue) {
return previousValue + Math.round(currentValue);
} // Create an array.
var numbers = [10.9, 15.4, 0.5]; // Call the reduce method, starting with an initial value of 0.
var result = numbers.reduce(addRounded, 0); document.write (result);
// Output: 27
下面的示例向数组中添加值。 currentIndex 和 array1 参数用于回调函数。
function addDigitValue(previousValue, currentDigit, currentIndex, array) {
var exponent = (array.length - 1) - currentIndex;
var digitValue = currentDigit * Math.pow(10, exponent);
return previousValue + digitValue;
}
var digits = [4, 1, 2, 5];
// Determine an integer that is computed from values in the array.
var result = digits.reduce(addDigitValue, 0);
document.write (result);
// Output: 4125
下面的示例获取一个数组,该数组仅包含另一个数组中的介于 1 和 10 之间值。提供给 reduce 方法的初始值是一个空数组。
function Process(previousArray, currentValue) {
// If currentValue is between 1 and 10,
// append currentValue to the array.
var nextArray;
if (currentValue >= 1 && currentValue <= 10)
nextArray = previousArray.concat(currentValue);
else
nextArray = previousArray;
// If this is not the last call by the reduce method,
// the returned array is previousArray on the next call.
// If this is the last call by the reduce method, the
// returned array is the return value of the reduce method.
return nextArray;
}
// Create an array.
var numbers = [20, 1, -5, 6, 50, 3];
// Call the reduce method, starting with an initial empty array.
var emptyArray = new Array();
var resultArray = numbers.reduce(Process, emptyArray);
document.write("result array=" + resultArray);
// Output:
// result array=1,6,3
reduce 方法 (Array) (JavaScript)的更多相关文章
- lastIndexOf 方法 (Array) (JavaScript)
lastIndexOf 方法 (Array) (JavaScript) 返回指定的值在数组中的最后一个匹配项的索引. 语法 array1.lastIndexOf(searchEleme ...
- forEach 方法 (Array) (JavaScript)
为数组中的每个元素执行指定操作. 语法 array1.forEach(callbackfn[, thisArg]) 参数 参数 定义 array1 必选.一个数组对象. callbackfn 必选.最 ...
- JavaScript - reduce方法,reduceRight方法 (Array)
JavaScript - reduce方法 (Array) 解释:reduce() 方法接收一个函数作为累加器(accumulator),数组 中的每个值(从左到右)开始合并,最终为一个值. 语法:a ...
- es 5 数组reduce方法记忆
reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值. 概念:对数组中的所有元素调用指定的回调函数.该回调函数的返回值为累积结果,并 ...
- JavaScript Array every()&some()&reduce()方法
every()方法测试数组的所有元素是否都通过了指定函数的测试. // 每一项都要满足条件才会返回true,只要有一项不满足返回false var arr = [1, 2, 3, 4]; let bl ...
- JavaScript中reduce()方法
原文 http://aotu.io/notes/2016/04/15/2016-04-14-js-reduce/ JavaScript中reduce()方法不完全指南 reduce() 方法接收 ...
- 在JavaScript函数式编程里使用Map和Reduce方法
所有人都谈论道workflows支持ECMAScript6里出现的令人吃惊的新特性,因此我们很容易忘掉ECMAScript5带给我们一些很棒的工具方法来支持在JavaScript里进行函数编程,这些工 ...
- JavaScript reduce() 方法
转载:http://www.runoob.com/jsref/jsref-reduce.html JavaScript Array 对象 实例 计算数组元素相加后的总和: var numbers = ...
- JavaScript数组的reduce方法详解
数组经常用到的方法有push.join.indexOf.slice等等,但是有一个经常被我们忽略的方法:reduce,这个方法简直强大的不要不要的. 我们先来看看这个方法的官方概述:reduce() ...
随机推荐
- 【java设计模式】【创建模式Creational Pattern】简单工厂模式Simple Factory Pattern(静态工厂方法模式Static Factory Method Pattern)
public class Test { public static void main(String[] args){ try{ Factory.factory("A").doSt ...
- Django ModelForm修改默认的控件属性
Django 中利用ModelForm 可以快速地利用数据库对应的Model 子类来自动创建对应表单. 例如: from django.db import models from django.for ...
- async和enterproxy控制并发数量
聊聊并发与并行 并发我们经常提及之,不管是web server,app并发无处不在,操作系统中,指一个时间段中几个程序处于已经启动运行到完毕之间,且这几个程序都是在同一处理机上运行,并且任一个时间点只 ...
- ArcGIS 网络分析[1.3] 在个人地理数据库中创建网络数据集/并简单试验最佳路径
上篇使用shp文件创建网络数据集,然而在ArcGIS 9中就支持地理数据库了,数据库的管理更为科学强大. 本篇就使用个人地理数据库进行建立网络数据集,线数据仍然可以是1.1中的线数据,但是我做了一些修 ...
- solr安装配置
1.solr是基于tomcat安装部署的 2.网上下载solr-5.2.1 http://lucene.apache.org/solr/downloads.html 3.解压solr文件 tar zx ...
- AdaBoost对实际数据分类的Julia实现
写在前面 AdaBoost是机器学习领域一个很重要很流行的算法,而Julia是一门新兴的发展迅速的科学计算语言.本文将从一个实际例子出发,展示如何用Julia语言实现AdaBoost算法. 什么是Ad ...
- python爬虫爬取大众点评并导入redis
直接上代码,导入redis的中文编码没有解决,日后解决了会第一时间上代码!新手上路,多多包涵! # -*- coding: utf-8 -*- import re import requests fr ...
- Java设计模式之(一)------单例模式
1.什么是单例模式? 采取一定的办法保证在整个软件系统中,单例模式确保对于某个类只能存在一个实例.有如下三个特点: ①.单例类只能有一个实例 ②.单例类必须自己创建自己的实例 ③.单例类必须提供外界获 ...
- git 分支操作
查看git分支: git fetch刷新git git branch -a 列出所有的分支 git checkout origin/要切换的分支 git branch -r 查看远程分支 git c ...
- 6、投资的一些思考 - CEO之公司管理经验谈
对于投资,前面笔者写过一个文:IT人经济思维之投资 - 创业与投资系列文章 ,里面列举了笔者自己做过的投资方面的内容.今天就说说公司投资的一些思考问题. 公司投资的问题,笔者还是那句话:关键是找出适合 ...
注意