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 > |