Reduce Method in JavaScript – Unique Array Information
Posted: Sun Feb 02, 2025 7:05 am
Learn how to use the reduce method in JavaScript. This is a method that allows you to extract unique information from an array.
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
To receive the file(s) used in the class by email, fill in :
How to use JavaScript in any company and any area, with examples fully applied to the reality of the Job Market, whether to become a Front End, Backend, Full Stack or Mobile Developer.
Start now
Right arrow
JavaScript icon used as backgroundThree images of JavaScript course classes
Reduce Method in JavaScript – Unique Array Information
Today, I'm going to show you how to use the reduce chinese america data method in JavaScript to extract unique information from an array.
With this method, you can extract, from an array , unique values such as the largest value, smallest, sum of values, average, and so on.
This is a very important method that can help you a lot in your next projects. So, download the available material and come with me as I show you 4 examples so you can understand how it works in practice.
Reduce Method in JavaScript
Reduce is another method available in JavaScript for working with arrays. This method extracts unique information from a list and can be used to extract the largest or smallest value, the average, sum, and so on.
This method can be used in a variety of scenarios. For example, imagine that you work for a sales company and have a database or spreadsheet with product sales records. With this method, you can find out the highest sales value, the lowest value, the total revenue, among other relevant information.
To see this method in practice, let's use as an example a list of sales made by the company's employees throughout the day. From this, we will define the value of the largest and smallest sales of that day, the total revenue and the average for each salesperson.
We can start by defining our array:
In this array, each element corresponds to the value sold by an employee throughout the day.
JavaScript Reduce Method – Highest Value
Let's start by extracting the largest value from our array. To do this, we will use the reduce method . This method receives as arguments a callback function , which will be an arrow function , and the initial value, which was set to 0 .
The defined function will be applied to each element of the array to search for the desired unique information, in this case, the largest value.
To do this, it compares the current value being traversed with the highest stored value. If the current element is greater than the stored value, this value is replaced and updated.
Since we set the initial value to 0, the function will start by comparing the first value in the array to 0.
We will store the result of this function inside the variable maiorValor .
Our arrow function receives two parameters : largestValue , which starts at 0, and currentElement , the value of the array that is currently being traversed.
From this, we compare whether the largestValue is greater than the currentElement . If this is true , it returns the largestValue, otherwise it returns the currentElement.
So, after the first iteration of the function, the value of the variable largestValue will become 650 , because 650 is greater than 0. In the second iteration, it will continue to be 650 , because 550 is less than 650. In the third iteration, it will be updated to 1020 . And so on.
To view the final result of this method, we can display the final value stored in the variable using the function .
If you prefer this content in video lesson format, watch the video below or access our YouTube channel !
To receive the file(s) used in the class by email, fill in :
How to use JavaScript in any company and any area, with examples fully applied to the reality of the Job Market, whether to become a Front End, Backend, Full Stack or Mobile Developer.
Start now
Right arrow
JavaScript icon used as backgroundThree images of JavaScript course classes
Reduce Method in JavaScript – Unique Array Information
Today, I'm going to show you how to use the reduce chinese america data method in JavaScript to extract unique information from an array.
With this method, you can extract, from an array , unique values such as the largest value, smallest, sum of values, average, and so on.
This is a very important method that can help you a lot in your next projects. So, download the available material and come with me as I show you 4 examples so you can understand how it works in practice.
Reduce Method in JavaScript
Reduce is another method available in JavaScript for working with arrays. This method extracts unique information from a list and can be used to extract the largest or smallest value, the average, sum, and so on.
This method can be used in a variety of scenarios. For example, imagine that you work for a sales company and have a database or spreadsheet with product sales records. With this method, you can find out the highest sales value, the lowest value, the total revenue, among other relevant information.
To see this method in practice, let's use as an example a list of sales made by the company's employees throughout the day. From this, we will define the value of the largest and smallest sales of that day, the total revenue and the average for each salesperson.
We can start by defining our array:
In this array, each element corresponds to the value sold by an employee throughout the day.
JavaScript Reduce Method – Highest Value
Let's start by extracting the largest value from our array. To do this, we will use the reduce method . This method receives as arguments a callback function , which will be an arrow function , and the initial value, which was set to 0 .
The defined function will be applied to each element of the array to search for the desired unique information, in this case, the largest value.
To do this, it compares the current value being traversed with the highest stored value. If the current element is greater than the stored value, this value is replaced and updated.
Since we set the initial value to 0, the function will start by comparing the first value in the array to 0.
We will store the result of this function inside the variable maiorValor .
Our arrow function receives two parameters : largestValue , which starts at 0, and currentElement , the value of the array that is currently being traversed.
From this, we compare whether the largestValue is greater than the currentElement . If this is true , it returns the largestValue, otherwise it returns the currentElement.
So, after the first iteration of the function, the value of the variable largestValue will become 650 , because 650 is greater than 0. In the second iteration, it will continue to be 650 , because 550 is less than 650. In the third iteration, it will be updated to 1020 . And so on.
To view the final result of this method, we can display the final value stored in the variable using the function .