JavaScript Array reduceRight() methode
- Previous page reduce()
- Next page reverse()
- Go back to the previous level JavaScript Array Referentie Handleiding
Definitie en gebruik
reduceRight()
De methode vermindert de array tot een enkele waarde.
reduceRight()
De methode voert de gegeven functie uit voor elke waarde van de array (van rechts naar links).
De retourwaarde van de functie wordt opgeslagen in de accumulator (resultaat/totaal).
Opmerking:Voer geen actie uit voor arrayelementen zonder waarde reduceRight()
Methode.
Voorbeeld
Voorbeeld 1
Verwijder de cijfers in de array, beginnend aan het einde:
var numbers = [175, 50, 25]; document.getElementById("demo").innerHTML = numbers.reduceRight(myFunc); function myFunc(total, num) { return total - num; }
例子 2
从右到左减去数字,并显示总和:
<button onclick="myFunction()">Try it</button> <p>Sum of numbers in array: <span id="demo"></span></p> <script> var numbers = [2, 45, 30, 100]; function getSum(total, num) { return total - num; } function myFunction(item) { document.getElementById("demo").innerHTML = numbers.reduceRight(getSum); } </script>
语法
array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)
参数值
参数 | 描述 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
function(total, currentValue, index, arr) | 必需。为数组中的每个元素运行的函数。
函数参数:
|
||||||||||
initialValue | 可选。作为初始值传递给函数的值。 |
技术细节
返回值: | 返回上次调用回调函数的累积结果。 |
---|---|
JavaScript 版本: | ECMAScript 5 |
浏览器支持
表格中的数字注明了完全支持该方法的首个浏览器版本。
所有浏览器都完全支持 reduceRight()
方法:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome 3 | IE 9 | Edge 12 | Firefox 3 | Safari 5 | Opera 10.5 |
2009年6月 | 2010年9月 | 2015年7月 | 2009年1月 | 2010年6月 | 2010年3月 |
相关页面
Tutorial:JavaScript array
Tutorial:JavaScript array Const
Tutorial:JavaScript array methods
Tutorial:JavaScript sort array
Tutorial:JavaScript array iteration
Manual:Array.reduce() method
- Previous page reduce()
- Next page reverse()
- Go back to the previous level JavaScript Array Referentie Handleiding