| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. Its 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]
arr.reduceRight(callback[, initialValue])
callback4 ._ previousValue
_ : initialValue. ( ).
currentValue.
index.
arrayreduce .
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]
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 2027 LanguageSpecification # sec-array.prototype.reduceright |
This page was last modified on 2025 10 10 by MDN contributors.
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/FunctionObject.prototype.__defineGetter__()Object.prototype.__defineSetter__()Object.prototype.__lookupGetter__()Object.prototype.__lookupSetter__()Object.prototype.hasOwnProperty()Object.prototype.isPrototypeOf()Object.prototype.propertyIsEnumerable()Object.prototype.toLocaleString()Object.prototype.toString()Object.prototype.valueOf()Your blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |