Computer >> Computer tutorials >  >> Programming >> Javascript

Write the use of Array.reduceRight() method in JavaScript?


array.reduceRight()

The array.reduceRight() is an inbuilt function in JavaScript which is used to convert elements of the given array from right to left to a single value.It accepts 2 parameters( current value and previous value ) from the given array and executes the operation.In the following example all the elements(group of arrays) were converted in to a single group (9,10,x,y,z,1,2,3) from right to left, when reduceRight() method is applied.

Example

<html>
<body>
<script>
   const Arr = [ [ 1, 2, 3 ], [ "x", "y", "z" ], [ 9, 10 ] ];
   array = Arr.reduceRight((previousValue, currentValue) => previousValue.concat(currentValue));
   document.write(array);
</script>
</body>
</html>

output

9,10,x,y,z,1,2,3