Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypedArray.prototype.reduce()

BaselineWidely available

Thereduce() method ofTypedArray instances executes a user-supplied "reducer" callback function on each element of the typed array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the typed array is a single value. This method has the same algorithm asArray.prototype.reduce().

Try it

const uint8 = new Uint8Array([0, 1, 2, 3]);function sum(accumulator, currentValue) {  return accumulator + currentValue;}console.log(uint8.reduce(sum));// Expected output: 6

Syntax

js
reduce(callbackFn)reduce(callbackFn, initialValue)

Parameters

callbackFn

A function to execute for each element in the typed array. Its return value becomes the value of theaccumulator parameter on the next invocation ofcallbackFn. For the last invocation, the return value becomes the return value ofreduce(). The function is called with the following arguments:

accumulator

The value resulting from the previous call tocallbackFn. On the first call, its value isinitialValue if the latter is specified; otherwise its value isarray[0].

currentValue

The value of the current element. On the first call, its value isarray[0] ifinitialValue is specified; otherwise its value isarray[1].

currentIndex

The index position ofcurrentValue in the typed array. On the first call, its value is0 ifinitialValue is specified, otherwise1.

array

The typed arrayreduce() was called upon.

initialValueOptional

A value to whichaccumulator is initialized the first time the callback is called.IfinitialValue is specified,callbackFn starts executing with the first value in the typed array ascurrentValue.IfinitialValue isnot specified,accumulator is initialized to the first value in the typed array, andcallbackFn starts executing with the second value in the typed array ascurrentValue. In this case, if the typed array is empty (so that there's no first value to return asaccumulator), an error is thrown.

Return value

The value that results from running the "reducer" callback function to completion over the entire typed array.

Exceptions

TypeError

Thrown if the typed array contains no elements andinitialValue is not provided.

Description

SeeArray.prototype.reduce() for more details. This method is not generic and can only be called on typed array instances.

Examples

Sum up all values within an array

js
const total = new Uint8Array([0, 1, 2, 3]).reduce((a, b) => a + b);// total === 6

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-%typedarray%.prototype.reduce

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp