| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | [Back] [Original] |
Get to know MDN better
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]
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
callbackFn callbackFn accumulator reduceRight()
accumulator callbackFn initialValue
currentValue initialValue 2
currentIndexcurrentValue initialValue array.length - 1 array.length - 2
reduceRight()
initialValue callbackFn reduceRight() TypeError
reduceRight() thisArg callbackFn undefined this callbackFn globalThis
reduce() reduce reduceRight JavaScript reduce reduceRight
reduceRight callbackFn
arr.reduceRight((accumulator, currentValue, index, array) => {
//
});
accumulator currentValue 2 initialValue reduceRight accumulator initialValue currentValue initialValue accumulator currentValue 2
initialValue TypeError () 1 initialValue initialValue callbackFn
[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() 2 initialValue 10
[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
const sum = [0, 1, 2, 3].reduceRight((a, b) => a + b);
// sum is 6
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);
};
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
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() undefined
console.log([1, 2, , 4].reduceRight((a, b) => a + b)); // 7
console.log([1, 2, undefined, 4].reduceRight((a, b) => a + b)); // NaN
reduceRight() this length length
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 |
Arrayat()concat()copyWithin()entries()every()fill()filter()find()findIndex()findLast()findLastIndex()flat()flatMap()forEach()includes()indexOf()join()keys()lastIndexOf()map()pop()push()reduce()reduceRight()reverse()shift()slice()some()sort()splice()toLocaleString()toReversed()toSorted()toSpliced()toString()unshift()values()with()[Symbol.iterator]()Object/Function| Web Proxy Viewer | New URL | Original Page |