Array reduceRight()
reduceRight() is reduction method. reduceRight() iterates all items and build up a value for return. reduceRight() starts at the last and travels toward the first.
reduceRight() accepts two arguments:
- a function to call on each item
- an optional initial value
The function passed into reduceRight() accepts four arguments:
- the previous value,
- the current value,
- the item's index,
- the array object.
The returned value from the function is passed in as the first argument for the next item. The first iteration occurs on the second item in the array.
The reduceRight() method works in the same way, just in the opposite direction.
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script type="text/javascript">
var values = [1,2,3,4,5];
var sum = values.reduceRight(function(prev, cur, index, array){
return prev + cur;
});
document.writeln(sum); //15
</script>
</head>
<body>
</body>
</html>
Home
JavaScript Book
Essential Types
JavaScript Book
Essential Types
Array:
- The Array Type
- Array Built-in Methods
- Detecting Arrays
- Get and set array values
- Enumerating the Contents of an Array
- Array Length
- Array join() method
- Array concat()
- Array indexOf()
- Array lastIndexOf()
- Array every()
- Array filter() filters array with the given function.
- Array map()
- Array forEach()
- push() and pop():Array Stack Methods
- push(), shift():Array Queue Methods
- Array reduce()
- Array reduceRight()
- reverse():Reordering array
- Array slice()
- Array some()
- Array splice()
- Array sort()
- toString(), toLocaleString() and valueOf Array
- Array unshift()