- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to apply function against an accumulator and each key of object in JavaScript?
In JavaScript, We can use the reduce() method to apply a function against an accumulator and each key of an object (from left to right).
The reduce() method is called on a given array and takes in a callback function as its first argument. Please refer to Array reduce() for more details. Let’s look at the syntax of this method.
Syntax
array array.reduce(callback[, initialValue]);
Parameters
callback − Function to execute on each value in the array.
initialValue − Object to use as the first argument to the first call of the callback.
The callback function is passed with four arguments
The first argument is the accumulator, which is the result of the previous callback function call, or the initial value if this is the first call.
The second argument is the current key being processed.
The third argument is the current value being processed.
The fourth argument is the current index being processed.
The reduce() method then returns the accumulator.
Applying reduce() Method
Let's take a look at a simple example to see how the reduce() method works.
We have an array of objects, each with a name and a value property. We use the reduce() method to sum up the values.
We can do that by passing in a callback function that takes in the accumulator (the result of the previous callback function call) and the current value being processed. The callback function then returns the accumulator plus the current value.
Example
<html> <head> <title>Examples</title> </head> <body> <div id="result"></div> <script> let arr = [{ name: "John", value: 5 }, { name: "Jane", value: 7 }, { name: "Jim", value: 3 }]; let sum = arr.reduce((acc, cur) => acc + cur.value, 0); document.getElementById("result").innerHTML = sum; </script> </body> </html>
In the program above, we have an array of objects, each with a name and a value property. We use the reduce() method to sum up the values.
We pass in a callback function ((acc, cur) => acc + cur.value) that takes in the accumulator, acc (the result of the previous callback function call) and the current value, cur, being processed. The callback function then returns the accumulator plus the current value.
The reduce() method then returns the accumulator, which is the sum of all the values.
Advantages of using reduce()
One advantage of using reduce() is that it can make your code more readable.
For example, if we want to sum up the values in the array above, we could also use a for loop.
Example
<html> <head> <title>Using for loop</title> </head> <body> <h2> Using for loop to sum the values of objects in an array</h2> <div id="result"></div> <script> let arr = [{ name: "John", value: 5 }, { name: "Jane", value: 7 }, { name: "Jim", value: 3 }]; let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i].value; } document.getElementById("result").innerHTML = sum </script> </body> </html>
Both the reduce() method and the for loop above achieve the same result. However, the reduce() method is more concise and easier to read.
Another advantage of using reduce() is that it can be used to process data in parallel.
For example, if we have an array of objects, each with a name and a value property, and we want to process each object in parallel, we could use the map() method.
let arr = [{ name: "John", value: 5 }, { name: "Jane", value: 7 }, { name: "Jim", value: 3 }]; let results = arr.map(obj => { // do something with obj });
However, if we want to process the data in sequence, we can use the reduce() method.
let arr = [{ name: "John", value: 5 }, { name: "Jane", value: 7 }, { name: "Jim", value: 3 }]; let result = arr.reduce((acc, obj) => { // do something with obj and return the result }, initialValue);
Conclusion
In this tutorial, we have seen how to use the reduce() method to apply a function against an accumulator and each key of an object. We have also seen some advantages of using reduce() over other methods.
- Related Articles
- How to apply bind() function in JavaScript?
- How to access an object value using variable key in JavaScript?
- JavaScript: How to Create an Object from Key-Value Pairs
- How to use array that include and check an object against a property of an object?
- How to modify key values in an object with JavaScript and remove the underscore?
- How to apply a function simultaneously against two values of the array from left-to-right?
- How to apply a function simultaneously against two values of the array from right-to-left?
- Retrieve key and values from object in an array JavaScript
- JavaScript Function Apply
- How to change an object Key without changing the original array in JavaScript?
- How to access an object having spaces in the object’s key using JavaScript?
- How to return an object from a JavaScript function?
- How to set a String as a key for an object - JavaScript?
- How to sort an object in ascending order by the value of a key in JavaScript?
- How to pass an object as a parameter in JavaScript function?
