首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

typedArray.reduceRight

reduceRight()在累加器和类型化数组的每个元素上(从右到左)调用函数,使其归约为单一的值。这个方法的算法和Array.prototype.reduceRight()相同。TypedArray是这里的类型化数组类型之一。

语法

代码语言:javascript
复制
typedarray.reduceRight(callback[, initialValue])

参数

callback对类型数组的每一个值应用的函数,它接受四个参数:previousValue回调函数上一次调用所返回的值,或者是提供的initialValuecurrentValue类型化数组中当前要处理的值。index类型化数组中要处理的当前元素的下标arrayreduceRight?在其上调用的类型化数组initialValue可选。用作callback首次调用的第一个参数的对象。

返回值

由归约返回的结果。

描述

reduceRight方法对类型化数组中出现的每个元素执行callback函数,除了类型化数组的空隙。它接受四个参数:初始值(或者来自之前callback调用的值),当前元素的值,当前下标,以及被遍历的类型化数组。

reduceRight回调函数的调用是这样:

代码语言:javascript
复制
typedarray.reduceRight(function(previousValue, currentValue, index, typedarray) {
  // ...
});

第一次调用回调函数的时候,previousValuecurrentValue可以是两个值之一。如果initialValuereduce的调用中提供,previousValue会等于initialValue并且currentValue会等于类型化数组的最后一个值。 如果initialValue没有提供,则previousValue等于类型化数组的最后一个值,currentValue会等于倒数第二个值。

如果类型化数组为空并且没有提供initialValue,会抛出TypeError。如果类型化数组中只有一个元素(无论位置)并且没有提供initialValue,或者如果提供了initialValue但是类型化数组为空,会返回唯一的值,但不会调用callback

示例

累加数组中的所有值

代码语言:javascript
复制
var total = new Uint8Array([0, 1, 2, 3]).reduceRight(function(a, b) {
  return a + b;
});
// total == 6

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of '%TypedArray%.prototype.reduceRight' in that specification.

Standard

Initial definition.

ECMAScript 2017 Draft (ECMA-262)The definition of '%TypedArray%.prototype.reduceRight' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

45

12

37 (37)

No support

32

10

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

?

?

37.0 (37)

?

?

?

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com