
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to apply the reduce method for objects in JavaScript?
Following is the code to apply reduce method to objects in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result,.sample { font-size: 18px; font-weight: 500; color: rebeccapurple; } .result { color: red; } </style> </head> <body> <h1>Reduce method for objects in JavaScript</h1> <div class="sample">{ a:6, b:2, c:9, d:5, }</div> <div class="result"><br /></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to multiply the above object values together</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let multiplyObj = { a: 6, b: 2, c: 9, d: 5, }; BtnEle.addEventListener("click", () => { let multiplyTotal = Object.values(multiplyObj).reduce((a, b) => a * b); resEle.innerHTML += "Multiplication of the object values = " + multiplyTotal; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Average with the Reduce Method in JavaScript
- How to convert array into array of objects using map() and reduce() in JavaScript
- How to decode JSON objects in Laravel and apply for each loop on that?
- How to reduce arrays in JavaScript?
- How to use JavaScript map() method to access nested objects?
- What is a JavaScript apply() Method?
- What is the best way to reduce and merge a collection of objects – JavaScript?
- How to reduce the color using Iterator Method in OpenCV?
- How to apply bind() function in JavaScript?
- How to reduce color using Pointer Method in OpenCV?
- How to shallow copy the objects in JavaScript?
- How to create objects in JavaScript?
- How to workaround Objects vs arrays in JavaScript for key/value pairs?
- How to write shorthand for document.getElementById() method in JavaScript?
- How to apply for a job in railways?

Advertisements