JavaScript Array ลดด้านขวา ()
ตัวอย่าง
ลบตัวเลขในอาร์เรย์โดยเริ่มจากจุดสิ้นสุด:
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 |