[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight [Back]  [Original]

Array.prototype.reduceRight() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Array.prototype.reduceRight()

Baseline

20157

reduceRight() Array

Array.prototype.reduce()

const array = [
  [0, 1],
  [2, 3],
  [4, 5],
];

const result = array.reduceRight((accumulator, currentValue) =>
  accumulator.concat(currentValue),
);

console.log(result);
// : Array [4, 5, 2, 3, 0, 1]

js
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)

callbackFn

callbackFn accumulator reduceRight()

accumulator

callbackFn initialValue

currentValue

initialValue 2

currentIndex

currentValue initialValue array.length - 1 array.length - 2

reduceRight()

initialValue

callbackFn reduceRight() TypeError

reduceRight()

callbackFn

reduceRight() thisArg callbackFn undefined this callbackFn globalThis

reduceRight() this length

reduce() reduce reduceRight JavaScript reduce reduceRight

reduceRight()

reduceRight callbackFn

js
arr.reduceRight((accumulator, currentValue, index, array) => {
  // 
});

accumulator currentValue 2 initialValue reduceRight accumulator initialValue currentValue initialValue accumulator currentValue 2

initialValue TypeError () 1 initialValue initialValue callbackFn

js
[0, 1, 2, 3, 4].reduceRight(
  (accumulator, currentValue, index, array) => accumulator + currentValue,
);

4

accumulator currentValue index Return value
First call 4 3 3 7
Second call 7 2 2 9
Third call 9 1 1 10
Fourth call 10 0 0 10

reduceRight (10)

reduceRight()

reduceRight() 2 initialValue 10

js
[0, 1, 2, 3, 4].reduceRight(
  (accumulator, currentValue, index, array) => accumulator + currentValue,
  10,
);
accumulator currentValue index Return value
First call 10 4 4 14
Second call 14 3 3 17
Third call 17 2 2 19
Fourth call 19 1 1 20
Fifth call 20 0 0 20

reduceRight 20

js
const sum = [0, 1, 2, 3].reduceRight((a, b) => a + b);
// sum is 6

js
const waterfall =
  (...functions) =>
  (callback, ...args) =>
    functions.reduceRight(
      (composition, fn) =>
        (...results) =>
          fn(composition, ...results),
      callback,
    )(...args);

const randInt = (max) => Math.floor(Math.random() * max);

const add5 = (callback, x) => {
  setTimeout(callback, randInt(1000), x + 5);
};
const mul3 = (callback, x) => {
  setTimeout(callback, randInt(1000), x * 3);
};
const sub2 = (callback, x) => {
  setTimeout(callback, randInt(1000), x - 2);
};
const split = (callback, x) => {
  setTimeout(callback, randInt(1000), x, x);
};
const add = (callback, x, y) => {
  setTimeout(callback, randInt(1000), x + y);
};
const div4 = (callback, x) => {
  setTimeout(callback, randInt(1000), x / 4);
};

const computation = waterfall(add5, mul3, sub2, split, add, div4);
computation(console.log, 5); // 14 

// 

const computation2 = (input, callback) => {
  const f6 = (x) => div4(callback, x);
  const f5 = (x, y) => add(f6, x, y);
  const f4 = (x) => split(f5, x);
  const f3 = (x) => sub2(f4, x);
  const f2 = (x) => mul3(f3, x);
  add5(f2, input);
};

reduce reduceRight

js
const a = ["1", "2", "3", "4", "5"];
const left = a.reduce((prev, cur) => prev + cur);
const right = a.reduceRight((prev, cur) => prev + cur);

console.log(left); // "12345"
console.log(right); // "54321"

reduceRight()

Wikipedia Function composition

js
const compose =
  (...args) =>
  (value) =>
    args.reduceRight((acc, fn) => fn(acc), value);

// 
const inc = (n) => n + 1;

//  2 
const double = (n) => n * 2;

// 
console.log(compose(double, inc)(2)); // 6

// 
console.log(compose(inc, double)(2)); // 5

reduceRight()

reduceRight() undefined

js
console.log([1, 2, , 4].reduceRight((a, b) => a + b)); // 7
console.log([1, 2, undefined, 4].reduceRight((a, b) => a + b)); // NaN

reduceRight()

reduceRight() this length length

js
const arrayLike = {
  length: 3,
  0: 2,
  1: 3,
  2: 4,
  3: 99, // length  3  reduceRight() 
};
console.log(Array.prototype.reduceRight.call(arrayLike, (x, y) => x - y));
// -1  4 - 3 - 2

ECMAScript 2027 LanguageSpecification
# sec-array.prototype.reduceright


Web Proxy Viewer  |  New URL  |  Original Page