자바스크립트 배열 reduceRight()
예
끝에서 시작하여 배열의 숫자를 뺍니다.
const numbers = [175, 50, 25];
document.getElementById("demo").innerHTML
= numbers.reduceRight(myFunc);
function myFunc(total, num) {
return total - num;
}
오른쪽에서 왼쪽으로 숫자를 빼고 합계를 표시합니다.
const numbers = [2, 45, 30, 100];
document.getElementById("demo").innerHTML = numbers.reduceRight(getSum);
function getSum(total, num) {
return total - num;
}
정의 및 사용
이 reduceRight()
메서드는 각 배열 요소에 대해 감속기 기능을 실행합니다.
이 reduceRight()
방법은 오른쪽에서 왼쪽으로 작동합니다.
이 reduceRight()
메서드는 함수의 누적 결과라는 단일 값을 반환합니다.
메서드 는 reduceRight()
빈 요소에 대해 함수를 실행하지 않습니다.
또한보십시오:
통사론
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
매개변수
Parameter | Description | ||||||||
function() | Required. A function to be run for each element in the array. |
||||||||
Reducer function parameters:
| |||||||||
initialValue | Optional. A value to be passed to the function as the initial value |
반환 값
콜백 함수의 마지막 호출에서 누적된 결과입니다. |
브라우저 지원
reduceRight()
ECMAScript5(ES5) 기능입니다.
모든 브라우저에서 완전히 지원되는 ES5(JavaScript 2009):
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |