TypeScript Array reduce() Method
Last Updated :
03 Jan, 2023
The Array.reduce() is an inbuilt TypeScript function which is used to apply a function against two values of the array as to reduce it to a single value.
Syntax:
array.reduce(callback[, initialValue])
Parameter: This method accept two parameter as mentioned and described below:
- callback : This parameter is the Function to execute on each value in the array.
- initialValue : This parameter is the Object to use as the first argument to the first call of the callback.
Return Value: This method returns the reduced single value of the array.
Below examples illustrate the Array reduce() method in TypeScript
Example 1:
JavaScript
<script>
var arr = [ 11, 89, 23, 7, 98 ];
var val = arr.reduce( function (a, b)
{
return a + b;
});
console.log( val );
</script>
|
Output:
228
Example 2:
JavaScript
<script>
var arr = [2, 5, 6, 3, 8, 9];
var val;
val = arr.reduce( function (a, b)
{
return a*b/2;
});
console.log( val );
</script>
|
Output:
405
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...