
- 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
Sum of nested object values in Array using JavaScript
Following is the code to sum nested object values in array using 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 { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Sum of nested object values in Array</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3> Click on the above button to sum the nested object values of json array </h3> <script> let json = { storeData: [ { items: [ { itemID: 12, cost: { costNum: 100, }, }, { itemID: 22, cost: { costNum: 250, }, }, { itemID: 19, cost: { costNum: 350, }, }, ], }, ], }; let resEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { let sum = 0; json.storeData.map((ele) => ({ itemPrice: ele.items.forEach((item) => { sum += item.cost.costNum; }), })); resEle.innerHTML += "Total CostNum = " + sum + "<br>"; }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Recursion - Sum Nested Array in JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Weight sum of a nested array in JavaScript
- List all duplicate values in a nested JavaScript object
- Filter nested object by keys using JavaScript
- How to sum all elements in a nested array? JavaScript
- Transform data from a nested array to an object in JavaScript
- Finding the sum of unique array values - JavaScript
- JavaScript recursive loop to sum all integers from nested array?
- Converting a JavaScript object to an array of values - JavaScript
- Sum all duplicate values in array in JavaScript
- Print JSON nested object in JavaScript?
- Changing value of nested object keys in JavaScript
- Filter unique array values and sum in JavaScript
- Test for existence of nested JavaScript object key in JavaScript

Advertisements