Updated April 6, 2023
Introduction to TypeScript reduce
Whenever there is a need to reduce the multiple values stored in the array to a single value by performing a function on two elements of the array at once starting from the left, we make use of a function called reduce function in TypeScript. It takes two parameters, namely a function along with parameters which are going to be called back again and again and then the return value from this function which acts as a first parameter to the function in the next call back, and this reduce function returns a single value as a result after performing the function on all the elements of the array considering two elements at once.
Syntax to declare reduce function in TypeScript:
reduce(function_that_is_called_again_and_again(parameter1, parameter2)
{
return parameter1
}
Where function_that_is_called_again_and_again is the function along with parameters paramter1 and parameter2, which is going to be called back again and again until all the elements of the array are traversed, and then the return value from this function acts as the first parameter to the function in the next call back.
Working of reduce Function in TypeScript
- An array in TypeScript is used to store different values of similar datatypes in a linear fashion.
- If a need arises to reduce the multiple values stored in the array to a single value, we use a function called reduce function in TypeScript.
- The reduce function performs operation or function on two values of the array at a time, starting from left to the right.
- The reduce function takes two parameters, namely a function and then the return value from the function, which acts as the first parameter to the function itself in its next call back.
- The function passed as a parameter to the reduce function keeps executing again and again until all the elements of the array are traversed.
- The reduce function returns a single value resulting from performing an operation on the elements of the array.
Examples of TypeScript reduce
Given below are the examples of TypeScript reduce:
Example #1
TypeScript program demonstrates reduced function, which multiplies two elements of the array at once to find out the product of all the elements in the array and display the resulting value as the output on the screen.
Code:
//defining an array called arrayname to store the elements in the array
var arrayname = [ 1, 2, 5, 10 ];
//defining a variable called result to store the return result from reduce function whose firs t parameter is a function to compute the product of two numbers and their return result is passed as a first parameter to the function itself
var result = arrayname.reduce(function(firstparam, secondparam)
{
return firstparam * secondparam;
});
//displaying the result of the reduce function as the output on the screen
console.log('The product of all the elements of the array is: ');
console.log( result );
Output:
In the above program, an array called arrayname is defined to store the elements in the array. A variable called result is defined to store the return result from a reduced function whose first parameter is a function to compute the product of two numbers. Their return result is passed as a first parameter to the function itself. Then the result of the reduce function is displayed as the output on the screen.
Example #2
TypeScript program demonstrates reduced function, which divides two elements of the array at once to find out the result of performing division on all the elements in the array and display the resulting value as the output on the screen.
Code:
//defining an array called arrayname to store the elements in the array
var arrayname = [ 16, 4, 2 ];
//defining a variable called result to store the return result from reduce function whose firs t parameter is a function to compute the division of two numbers and their return result is passed as a first parameter to the function itself
var result = arrayname.reduce(function(firstparam, secondparam)
{
return firstparam / secondparam;
});
//displaying the result of the reduce function as the output on the screen
console.log('The result of performing division on all the elements of the array by taking two elements at once is:');
console.log( result );
Output:
In the above program, an array called arrayname is defined to store the elements in the array. A variable called result is defined to store the return result from a reduced function whose first parameter is a function to compute the division of two numbers. Their return result is passed as a first parameter to the function itself. Then the result of the reduce function is displayed as the output on the screen.
Rules and Regulations
Given below are the rules and regulations for using reduce function in TypeScript:
- The parameters to the reduce function must be a callback function whose return result acts as a first parameter to the function itself.
- The reduce function can be used on an array consisting of more than one value only.
- The reduce function can perform the function on only two elements of the array at once.
- The reduce function can perform the function on two elements of the array at once, starting from left to right.
- The resulting value from the reduce function can be of any data type like float, double etc.
- The resulting value is infinity in cases when the function cannot be performed on the elements of the array.
Recommended Articles
We hope that this EDUCBA information on “TypeScript reduce” was beneficial to you. You can view EDUCBA’s recommended articles for more information.