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() ë©ìëë ëì 기ì ëí´ í¨ì를 ì ì©íê³ ë°°ì´ì ê° ê° (ì¤ë¥¸ìª½ìì ì¼ìª½ì¼ë¡)ì ê°ì ë¨ì¼ ê°ì¼ë¡ ì¤ì¬ì¼í©ëë¤.
ìëí´ ë³´ê¸°
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]
ì¼ìª½ìì ì¤ë¥¸ìª½ì¼ë¡ Array.prototype.reduce()ë 참조íììì¤.
구문
arr.reduceRight(callback[, initialValue])
매ê°ë³ì
callback-
4 ê°ì ì¸ì를 ì·¨íì¬ ë°°ì´ì ê° ê°ì ëí´ ì¤íí í¨ìì ëë¤._
previousValue_ : ì½ë°±ì ë§ì§ë§ í¸ì¶ìì ì´ì ì ë¦¬í´ ë ê° ëë ì ê³µë ê²½ì° initialValue. (ìë 참조).currentValue-
ë°°ì´ìì ì²ë¦¬ì¤ì¸ íì¬ ììì ëë¤.
index-
ë°°ì´ìì ì²ë¦¬ì¤ì¸ íì¬ ììì ì¸ë±ì¤ì ëë¤.
array-
ë°°ì´ reduceê° í¸ì¶ëììµëë¤.
initialValue-
ì í 과목. ì½ë°±ì ìµì´ì í¸ì¶ì ìµì´ì ì¸ìë¡ì ì¬ì©íë ê°ì²´ì ëë¤.
ë°í ê°
ëì ê³ì°ì 결과를 ë°íí©ëë¤.
ì¤ëª
reduceRightë ë°°ì´ì 구ë©ì ì ì¸íê³ ë°°ì´ììë ê° ììì ëí´ ì½ë°± í¨ì를 í ë² ì¤íí©ëë¤.ì´ ì¸ìë ì´ê¸° ê° (ëë ì´ì ì½ë°± í¸ì¶ì ê°), íì¬ ììì ê°, íì¬ ì¸ë±ì¤ ë° ë°ë³µì´ ì¼ì´ëë ë°°ì´.
reduceRight ì½ë°± í¸ì¶ì ë¤ìê³¼ ê°ìµëë¤.
array.reduceRight(function (previousValue, currentValue, index, array) {
// ...
});
í¨ìê° ì²ì í¸ì¶ ë ë previousValue ë° currentValueë ë ê° ì¤ íëê° ë ì ììµëë¤. reduceValueì ëí í¸ì¶ì initialValueê° ì ê³µë ê²½ì° previousValueë initialValueì ê°ê³ currentValueë ë°°ì´ì ë§ì§ë§ ê°ê³¼ ê°ìµëë¤. initialValueê° ì ê³µëì§ ìì¼ë©´ previousValueë ë°°ì´ì ë§ì§ë§ ê°ê³¼ ê°ê³ currentValueë ë ë²ì§¸ - ë§ì§ë§ ê°ê³¼ ê°ìµëë¤.
ë°°ì´ì´ ë¹ì´ ìê³ initialValueê° ì ê³µëì§ ìì¼ë©´ TypeErrorê° ë°ìí©ëë¤. ë°°ì´ì ììê° 1 ê°ë§ ìì´ë (ìì¹ì ê´ê³ìì´) initialValueê° ì ê³µëì§ ììë ê²½ì°, ëë initialValueê° ì¤ë¹ëì´ ìì§ë§ ë°°ì´ì´ ë¹ì´ìë ê²½ì°, ì½ë°±ì í¸ì¶íì§ ìê³ ìë¡ ê°ì´ ë°íë©ëë¤.
í¨ìì ì¼ë¶ ì¤í ìì ë ë¤ìê³¼ ê°ìµëë¤.
[0, 1, 2, 3, 4].reduceRight(
function (previousValue, currentValue, index, array) {
return previousValue + currentValue;
},
);
ì½ë°±ì ë¤ ë² í¸ì¶ëë©° ê° í¸ì¶ì ì¸ìì ë°í ê°ì ë¤ìê³¼ ê°ìµëë¤.
previousValue |
currentValue |
index |
array |
return value | |
|---|---|---|---|---|---|
| first call | 4 |
3 |
3 |
[0, 1, 2, 3, 4] |
7 |
| second call | 7 |
2 |
2 |
[0, 1, 2, 3, 4] |
9 |
| third call | 9 |
1 |
1 |
[0, 1, 2, 3, 4] |
10 |
| fourth call | 10 |
0 |
0 |
[0, 1, 2, 3, 4] |
10 |
reduceRightì ìí´ ë°í ë ê°ì ë§ì§ë§ ì½ë°± í¸ì¶ (10)ì ê°ì´ë©ëë¤.
initialValue를 ì ê³µíë©´ ê²°ê³¼ë ë¤ìê³¼ ê°ìµëë¤.
[0, 1, 2, 3, 4].reduceRight(function (
previousValue,
currentValue,
index,
array,
) {
return previousValue + currentValue;
}, 10);
previousValue |
currentValue |
index |
array |
return value | |
|---|---|---|---|---|---|
| first call | 10 |
4 |
4 |
[0, 1, 2, 3, 4] |
14 |
| second call | 14 |
3 |
3 |
[0, 1, 2, 3, 4] |
17 |
| third call | 17 |
2 |
2 |
[0, 1, 2, 3, 4] |
19 |
| fourth call | 19 |
1 |
1 |
[0, 1, 2, 3, 4] |
20 |
| fifth call | 20 |
0 |
0 |
[0, 1, 2, 3, 4] |
20 |
reduceRightì ìí´ ì´ë²ì ë°í ë ê°ì ë¬¼ë¡ 20ì ëë¤.
ìì
>ë°°ì´ ë´ ëª¨ë ê°ì í©ê³ 구í기
var sum = [0, 1, 2, 3].reduceRight(function (a, b) {
return a + b;
});
// sum is 6
ì´ì¤ ë°°ì´ ì ê°í기
var flattened = [
[0, 1],
[2, 3],
[4, 5],
].reduceRight(function (a, b) {
return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
reduceì reduceRightì ì°¨ì´ì
var a = ["1", "2", "3", "4", "5"];
var left = a.reduce(function (prev, cur) {
return prev + cur;
});
var right = a.reduceRight(function (prev, cur) {
return prev + cur;
});
console.log(left); // "12345"
console.log(right); // "54321"
í´ë¦¬í
reduceRightë 5 íìì ECMA-262 íì¤ì ì¶ê°ëììµëë¤. íì¤ì 모ë 구íì ì¡´ì¬íì§ ìì ìë ììµëë¤. ì´ ë¬¸ì 를 í´ê²°íë ¤ë©´ ì¤í¬ë¦½í¸ ìì ë¶ë¶ì ë¤ì ì½ë를 ì½ì
íì¬ reduceRight를 기본ì ì¼ë¡ ì§ìíì§ ìë 구íìì ì¬ì©í ììê²íììì¤.
// ECMA-262, 5 í, 15.4.4.22ì ì ì ë¨ê³
// 참조 : http://es5.github.io/#x15.4.4.22
if ("function" !== typeof Array.prototype.reduceRight) {
Array.prototype.reduceRight = function (callback /*, initialValue*/) {
"use strict";
if (null === this || "undefined" === typeof this) {
throw new TypeError("Array.prototype.reduce called on null or undefined");
}
if ("function" !== typeof callback) {
throw new TypeError(callback + " is not a function");
}
var t = Object(this),
len = t.length >>> 0,
k = len - 1,
value;
if (arguments.length >= 2) {
value = arguments[1];
} else {
while (k >= 0 && !(k in t)) {
k--;
}
if (k < 0) {
throw new TypeError("Reduce of empty array with no initial value");
}
value = t[k--];
}
for (; k >= 0; k--) {
if (k in t) {
value = callback(value, t[k], k, t);
}
}
return value;
};
}
ëª ì¸
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-array.prototype.reduceright > |