forEach【要素処理】
reduce【隣接要素処理 (順方向)】
reduceRight【隣接要素処理 (逆方向)】

%TypedArray%.prototype.forEach【要素処理】

メモ

概要

  • 各要素を指定関数で処理
    • Array【配列】オブジェクトの forEach【要素処理】に類似
    • コールバック関数で配列の変更も可能

関連

外部リンク

構文

typedArray.forEach( callbackfn[, thisArg] )

callbackfn コールバック関数 (下記参照)
thisArg コールバック関数内で this で参照されるオブジェクト

TypeError 
    ・callbackfn (コールバック関数)が呼び出し可能な関数オブジェクト以外
callbackfn (コールバック関数)
引数名 (例)説明
value要素値
index要素インデックス
typedArray配列 オブジェクト
戻り値型説明
なし

function callbackForEach(value, index, typedArray) {
  console.log(`[${index}]:${value}`);
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray = new Int32Array(array);
typedArray.forEach(callbackForEach);
// 出力:[0]:1
// 出力:[1]:-6
// 出力:[2]:3
// 出力:[3]:-4
// 出力:[4]:5
// 出力:[5]:-2
// this 参照・配列変更 (1/2)
function callbackForEach2(value, index, typedArray) {
  console.log(`[${index}]:${value} (${this})`);
  typedArray[index] = value + this;
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray2 = new Int32Array(array);
typedArray2.forEach(callbackForEach2, 100);
// 出力:[0]:1 (100)
// 出力:[1]:-6 (100)
// 出力:[2]:3 (100)
// 出力:[3]:-4 (100)
// 出力:[4]:5 (100)
// 出力:[5]:-2 (100)

console.log(typedArray2);
// 出力:Int32Array(6) [ 101, 94, 103, 96, 105, 98 ]


// this 参照・配列変更 (2/2)
function callbackForEach3(value, index, typedArray) {
  console.log(`[${index}]:${value} (${this[index]})`);
  typedArray[index] = value + this[index];
}

const array31 = [1, -6, 3, -4, 5, -2];
const array32 = [100, -200, 300, -400, 500, -600];
let typedArray31 = new Int32Array(array31);
let typedArray32 = new Int32Array(array32);
typedArray31.forEach(callbackForEach3, typedArray32);
// 出力:[0]:1 (100)
// 出力:[1]:-6 (-200)
// 出力:[2]:3 (300)
// 出力:[3]:-4 (-400)
// 出力:[4]:5 (500)
// 出力:[5]:-2 (-600)

console.log(typedArray31);
// 出力:Int32Array(6) [ 101, -206, 303, -404, 505, -602 ]


// 例外
let typedArray4 = new Int32Array([1, -6, 3, -4, 5, -2]);
//typedArray4.forEach(123);
// 例外:TypeError: 123 is not a function

%TypedArray%.prototype.reduce【隣接要素処理 (順方向)】

メモ

概要

  • 各要素を順方向に処理 (直前要素の処理結果を引継ぎ)

関連

外部リンク

構文

typedArray.reduce( callbackfn[, initialValue] )

 最後のコールバック関数の戻り値 (空配列で初期値指定の場合、初期値)
callbackfn コールバック関数 (下記参照)
initialValue 初期値
    ・指定: 初回コールバック関数呼び出しの previousValue 引数
    ・省略:2つ目の要素からコールバック関数が呼び出され、1つ目の要素値が previousValue 引数

TypeError 
    ・callbackfn (コールバック関数)が呼び出し可能な関数オブジェクト以外
    ・空配列で初期値が未指定
callbackfn (コールバック関数)
引数名 (例)説明
previousValue指定された初期値 (初回) または 1つ前の呼び出し時の戻り値
currentValue現在の要素値
currentIndex現在の要素インデックス
typedArray配列 オブジェクト
戻り値型説明
任意次回の呼び出しpreviousValue 引数

function callbackReduce(previousValue, currentValue, currentIndex, typedArray) {
  console.log(`[${currentIndex}]:${currentValue} (${previousValue})`);
  return currentValue;
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray = new Int32Array(array);
let result = typedArray.reduce(callbackReduce);
// 出力:[1]:-6 (1)
// 出力:[2]:3 (-6)
// 出力:[3]:-4 (3)
// 出力:[4]:5 (-4)
// 出力:[5]:-2 (5)

console.log(result);
// 出力:-2
// 初期値・配列変更
function callbackReduce(previousValue, currentValue, currentIndex, typedArray) {
  console.log(`[${currentIndex}]:${currentValue} (${previousValue})`);
  typedArray[currentIndex] = Math.abs(currentValue) + Math.abs(previousValue * 100);
  return currentValue;
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray = new Int32Array(array);
let result = typedArray.reduce(callbackReduce, 9);
// 出力:[0]:1 (9)
// 出力:[1]:-6 (1)
// 出力:[2]:3 (-6)
// 出力:[3]:-4 (3)
// 出力:[4]:5 (-4)
// 出力:[5]:-2 (5)

console.log(result);
// 出力:-2

console.log(typedArray);
// 出力:Int32Array(6) [ 901, 106, 603, 304, 405, 502 ]


// 例外
//typedArray.reduce(123);
// 例外:TypeError: 123 is not a function

let typedArray2 = new Int32Array([]);
//typedArray2.reduce(callbackReduce);
// 例外:TypeError: reduce of empty array with no initial value

%TypedArray%.prototype.reduceRight【隣接要素処理 (逆方向)】

メモ

概要

関連

外部リンク

構文

typedArray.reduceRight( callbackfn[, initialValue] )

 最後のコールバック関数の戻り値 (空配列で初期値指定の場合、初期値)
callbackfn コールバック関数 (下記参照)
initialValue 初期値
    指定:初回コールバック関数呼び出しの previousValue 引数
    省略:最後から2つ目の要素からコールバック関数が呼び出され、最後の要素値が previousValue 引数

TypeError 例外
    ・callbackfn (コールバック関数)が呼び出し可能な関数オブジェクト以外
    ・空配列で初期値が未指定
callbackfn (コールバック関数)
引数名 (例)説明
previousValue指定された初期値 (初回) または 1つ前の呼び出し時の戻り値
currentValue現在の要素値
currentIndex現在の要素インデックス
typedArray配列 オブジェクト
戻り値型説明
任意次回の呼び出しpreviousValue引数

function callbackReduceRight(previousValue, currentValue, currentIndex, typedArray) {
  console.log(`[${currentIndex}]:${currentValue} (${previousValue})`);
  return currentValue;
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray = new Int32Array(array);
let result = typedArray.reduceRight(callbackReduceRight);
// 出力:[4]:5 (-2)
// 出力:[3]:-4 (5)
// 出力:[2]:3 (-4)
// 出力:[1]:-6 (3)
// 出力:[0]:1 (-6)

console.log(result);
// 出力:1
// 初期値・配列変更
function callbackReduceRight(previousValue, currentValue, currentIndex, typedArray) {
  console.log(`[${currentIndex}]:${currentValue} (${previousValue})`);
  typedArray[currentIndex] = Math.abs(currentValue) + Math.abs(previousValue * 100);
  return currentValue;
}

const array = [1, -6, 3, -4, 5, -2];
let typedArray = new Int32Array(array);
let result = typedArray.reduceRight(callbackReduceRight, 9);
// 出力:[5]:-2 (9)
// 出力:[4]:5 (-2)
// 出力:[3]:-4 (5)
// 出力:[2]:3 (-4)
// 出力:[1]:-6 (3)
// 出力:[0]:1 (-6)

console.log(result);
// 出力:1

console.log(typedArray);
// 出力:Int32Array(6) [ 601, 306, 403, 504, 205, 902 ]


// 例外
//typedArray.reduceRight(123);
// 例外:TypeError: 123 is not a function

let typedArray2 = new Int32Array([]);
//typedArray2.reduceRight(callbackReduceRight);
// 例外:TypeError: reduce of empty array with no initial value