Array.prototype.reduceRight()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itโs been available across browsers since โจ2015ๅนด7ๆโฉ.
reduceRight()
ๆนๆณๅฏน็ดฏๅ ๅจ๏ผaccumulator๏ผๅๆฐ็ป็ๆฏไธชๅผ๏ผๆไปๅณๅฐๅทฆ็้กบๅบ๏ผๅบ็จไธไธชๅฝๆฐ๏ผๅนถไฝฟๅ
ถๆไธบๅไธชๅผใ
ๅฏนไบไปๅทฆ่ณๅณ้ๅ็็ธไผผๆนๆณ๏ผ่ฏทๅ้
Array.prototype.reduce()
ใ
ๅฐ่ฏไธไธ
const array1 = [
[0, 1],
[2, 3],
[4, 5],
];
const result = array1.reduceRight((accumulator, currentValue) =>
accumulator.concat(currentValue),
);
console.log(result);
// Expected output: Array [4, 5, 2, 3, 0, 1]
่ฏญๆณ
reduceRight(callbackFn)
reduceRight(callbackFn, initialValue)
ๅๆฐ
callbackFn
-
ไธบๆฐ็ปไธญ็ๆฏไธชๅ ็ด ๆง่ก็ๅฝๆฐใๅ ถ่ฟๅๅผๅฐไฝไธบไธไธๆฌก่ฐ็จ
callbackFn
ๆถ็accumulator
ๅๆฐใๅฏนไบๆๅไธๆฌก่ฐ็จ๏ผ่ฟๅๅผๅฐๆไธบreduceRight()
็่ฟๅๅผใ่ฏฅๅฝๆฐ่ขซ่ฐ็จๆถๅฐไผ ๅ ฅไปฅไธๅๆฐ๏ผaccumulator
-
ไธไธๆฌก่ฐ็จ
callbackFn
็็ปๆใๅจ็ฌฌไธๆฌก่ฐ็จๆถ๏ผๅฆๆๆๅฎไบinitialValue
ๅไธบๆๅฎ็ๅผ๏ผๅฆๅไธบๆฐ็ปๆๅไธไธชๅ ็ด ็ๅผใ currentValue
-
ๆฐ็ปไธญๅฝๅๆญฃๅจๅค็็ๅ ็ด ใ
index
-
ๆญฃๅจๅค็็ๅ ็ด ๅจๆฐ็ปไธญ็็ดขๅผใ
array
-
่ฐ็จไบ
reduceRight()
็ๆฐ็ปๆฌ่บซใ
initialValue
ๅฏ้-
้ฆๆฌก่ฐ็จ
callbackFn
ๆถ็ดฏๅ ๅจ็ๅผใๅฆๆไธๆไพๅๅงๅผ๏ผๅๅฐไฝฟ็จๆฐ็ปไธญ็ๆๅไธไธชๅ ็ด ๏ผๅนถๅจ่ฟญไปฃๆถ่ทณ่ฟๅฎใๆฒกๆๅๅงๅผ็ๆ ๅตไธ๏ผๅจ็ฉบๆฐ็ปไธ่ฐ็จreduceRight()
ไผไบง็TypeError
ใ
่ฟๅๅผ
่ๅๅ็็ปๆๅผใ
ๆ่ฟฐ
reduceRight()
ๆนๆณๆฏไธไธช่ฟญไปฃๆนๆณใๅฎไธบๆฐ็ปไธญ็ๆๆๅ
็ด ้ๅบ่ฐ็จโreducerโๅ่ฐๅฝๆฐ๏ผๅนถๅฐๅฎไปฌ็ดฏ็งฏๅฐไธไธชๅไธ็ๅผไธญใ
callbackFn
ไป
ไธบๅทฒๅ้
ๅผ็ๆฐ็ป็ดขๅผ่ฐ็จใๅฎไธไผไธบ็จ็ๆฐ็ปไธญ็็ฉบๆงฝ่ฐ็จใ
ไธๅ
ถไป่ฟญไปฃๆนๆณไธๅ๏ผreduceRight()
ไธๆฅๅ thisArg
ๅๆฐใcallbackFn
่ฐ็จๆถๅง็ปไปฅ undefined
ไฝไธบ this
็ๅผ๏ผๅฆๆ callbackFn
ๆชๅคไบไธฅๆ ผๆจกๅผ๏ผๅ่ฏฅๅผๅฐ่ขซๆฟๆขไธบ globalThis
ใ
reduceRight()
ไธไผๆนๅ่ขซ่ฐ็จ็ๆฐ็ป๏ผไฝๆฏไฝไธบ callbackFn
ๆไพ็ๅฝๆฐๅฏ่ฝไผๆนๅๆฐ็ปใไฝ้่ฆๆณจๆ็ๆฏ๏ผๅจ็ฌฌไธๆฌก่ฐ็จ callbackFn
ไนๅ๏ผๆฐ็ป็้ฟๅบฆไผ่ขซไฟๅญใๅ ๆญค๏ผ
- ๅฝๅผๅง่ฐ็จ
reduceRight()
ๆถ๏ผcallbackFn
ๅฐไธไผ่ฎฟ้ฎ่ถ ๅบๆฐ็ปๅๅง้ฟๅบฆ็ไปปไฝๅ ็ด ใ - ๅฏนๅทฒ่ฎฟ้ฎ็ดขๅผ็ๆดๆนไธไผๅฏผ่ดๅๆฌกๅจ่ฟไบๅ
็ด ไธ่ฐ็จ
callbackFn
ใ - ๅฆๆๆฐ็ปไธญไธไธช็ฐๆ็ใๅฐๆช่ฎฟ้ฎ็ๅ
็ด ่ขซ
callbackFn
ๆดๆน๏ผๅๅฎไผ ้็ปcallbackFn
็ๅผๅฐๆฏ่ฏฅๅ ็ด ่ขซไฟฎๆนๅ็ๅผใ่ขซๅ ้ค็ๅ ็ด ๅไธไผ่ขซ่ฎฟ้ฎใ
่ญฆๅ๏ผ ไธ่ฟฐ็ฑปๅ็ๅนถๅไฟฎๆน็ปๅธธๅฏผ่ด้พไปฅ็่งฃ็ไปฃ็ ๏ผ้ๅธธๅบ้ฟๅ ๏ผ็นๆฎๆ ๅต้คๅค๏ผใ
reduceRight()
ๆนๆณๆฏ้็จ็ใๅฎๅชๆๆ this
ๅผๅ
ทๆ length
ๅฑๆงๅๆดๆฐ้ฎๅฑๆงใ
็คบไพ
ๆ ๅๅงๅผๆถ reduceRight() ๅฆไฝ่ฟ่ก
reduceRight()
ๆนๆณ่ฐ็จ callbackFn
็ๆ ผๅผๅฆไธ๏ผ
arr.reduceRight((accumulator, currentValue, index, array) => {
// โฆ
});
ๅจ็ฌฌไธๆฌก่ฐ็จๅฝๆฐๆถ๏ผaccumulator
ๅ currentValue
็ๅฏ่ฝๅๅผๆ
ๅตๆไธค็งใๅฆๆๅจ่ฐ็จ reduceRight
ๆถๆไพไบ initialValue
๏ผ้ฃไน accumulator
ๅฐ็ญไบ initialValue
๏ผ่ currentValue
ๅฐ็ญไบๆฐ็ปไธญ็ๆๅไธไธชๅผใๅฆๆๆฒกๆๆไพ initialValue
๏ผๅ accumulator
ๅฐ็ญไบๆฐ็ปไธญ็ๆๅไธไธชๅผ๏ผ่ currentValue
ๅฐ็ญไบๅๆฐ็ฌฌไบไธชๅผใ
ๅฆๆๆฐ็ปไธบ็ฉบไธๆฒกๆๆไพ initialValue
๏ผๅไผๆๅบ TypeError
ๅผๅธธใๅฆๆๆฐ็ปๅชๆไธไธชๅ
็ด ๏ผๆ ่ฎบๅ
ถไฝ็ฝฎๅฆไฝ๏ผไธๆฒกๆๆไพ initialValue
๏ผๆ่
ๆไพไบ initialValue
ไฝๆฐ็ปไธบ็ฉบ๏ผๅ็ดๆฅ่ฟๅ่ฏฅๅไธชๅผ๏ผไธ callbackFn
ไธไผ่ขซ่ฐ็จใ
่ฟไธชๅฝๆฐ็ไธไบ็คบไพ่ฟ่กๅฆไธ๏ผ
[0, 1, 2, 3, 4].reduceRight(
(accumulator, currentValue, index, array) => accumulator + currentValue,
);
ไธๅ ฑไผ่ฐ็จๅๆฌกๅ่ฐๅฝๆฐ๏ผๆฏๆฌก่ฐ็จ็ๅๆฐๅ่ฟๅๅผๅฆไธ๏ผ
accumulator |
currentValue |
index |
่ฟๅๅผ | |
---|---|---|---|---|
็ฌฌไธๆฌก่ฐ็จ | 4 |
3 |
3 |
7 |
็ฌฌไบๆฌก่ฐ็จ | 7 |
2 |
2 |
9 |
็ฌฌไธๆฌก่ฐ็จ | 9 |
1 |
1 |
10 |
็ฌฌๅๆฌก่ฐ็จ | 10 |
0 |
0 |
10 |
array
ๅๆฐๅจๆดไธช่ฟ็จไธญๅง็ปไธๅ๏ผๅง็ปไธบ [0, 1, 2, 3, 4]
ใreduceRight
่ฟๅ็ๅผๅฐๆฏๆๅไธๆฌกๅ่ฐๅฝๆฐ่ฐ็จ็่ฟๅๅผ๏ผ10
๏ผใ
ๆๅๅงๅผๆถ reduceRight() ๅฆไฝ่ฟ่ก
่ฟ้ๆไปฌไฝฟ็จ็ธๅ็็ฎๆณๅฏนๅไธๆฐ็ป่ฟ่ก reduce๏ผไฝๆฏๅฐ initialValue
ๅๆฐ่ฎพ็ฝฎไธบ 10
๏ผไฝไธบ็ฌฌไบไธชๅๆฐไผ ้็ป reduceRight()
ๆนๆณ๏ผ
[0, 1, 2, 3, 4].reduceRight(
(accumulator, currentValue, index, array) => accumulator + currentValue,
10,
);
accumulator |
currentValue |
index |
่ฟๅๅผ | |
---|---|---|---|---|
็ฌฌไธๆฌก่ฐ็จ | 10 |
4 |
4 |
14 |
็ฌฌไบๆฌก่ฐ็จ | 14 |
3 |
3 |
17 |
็ฌฌไธๆฌก่ฐ็จ | 17 |
2 |
2 |
19 |
็ฌฌๅๆฌก่ฐ็จ | 19 |
1 |
1 |
20 |
็ฌฌไบๆฌก่ฐ็จ | 20 |
0 |
0 |
20 |
่ฟๆฌก๏ผreduceRight
่ฟๅๅผไธบ 20
ใ
ๆฑไธไธชๆฐ็ปไธญๆๆๅผ็ๅ
const sum = [0, 1, 2, 3].reduceRight((a, b) => a + b);
// sum ็ๅผๆฏ 6
ๅฑๅนณไธไธชไบ็ปดๆฐ็ป
const arrays = [
[0, 1],
[2, 3],
[4, 5],
];
const flattened = arrays.reduceRight((a, b) => a.concat(b), []);
// flattened ็ๅผๆฏ [4, 5, 2, 3, 0, 1]
ไธฒ่่ฟ่กไธๅๅผๆญฅๅฝๆฐ๏ผๆฏไธชๅฝๆฐ้ฝๅฐๅ ถ็ปๆไผ ็ปไธไธไธชๅฝๆฐ
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 mult3 = (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, mult3, sub2, split, add, div4);
computation(console.log, 5); // Logs 14
// same as:
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) => mult3(f3, x);
add5(f2, input);
};
reduce
ไธ reduceRight
ไน้ด็ๅบๅซ
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()
ๆฅๅฎ็ฐๅฝๆฐ็ปๅใ
ๅ่ง็ปดๅบ็พ็งไธ็ๅฝๆฐ็ปๅใ
const compose =
(...args) =>
(value) =>
args.reduceRight((acc, fn) => fn(acc), value);
// Increment passed number
const inc = (n) => n + 1;
// Doubles the passed value
const double = (n) => n * 2;
// using composition function
console.log(compose(double, inc)(2)); // 6
// using composition function
console.log(compose(inc, double)(2)); // 5
ๅจ็จ็ๆฐ็ปไธญไฝฟ็จ reduceRight()
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()
reduceRight()
ๆนๆณ่ฏปๅ this
็ length
ๅฑๆง๏ผ็ถๅ่ฎฟ้ฎๆฏไธชๆดๆฐ็ดขๅผใ
const arrayLike = {
length: 3,
0: 2,
1: 3,
2: 4,
};
console.log(Array.prototype.reduceRight.call(arrayLike, (x, y) => x - y));
// -1, ๅณ 4 - 3 - 2
่ง่
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-array.prototype.reduceright |
ๆต่งๅจๅ ผๅฎนๆง
Loadingโฆ