
- 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
JavaScript Sum of two objects with same properties
Suppose, we have two objects like these −
const obj1 = { a:12, b:8, c:17 }; const obj2 = { a:2, b:4, c:1 };
We are required to write a JavaScript function that takes in two such object.
The function should sum the values of identical properties into a single property. Therefore, the final object should look something like this −
const output = { a:14, b:12, c:18 };
Note − For the sake of simplicity, we have just used two objects, but we are required to write our function such that it can take in any number of objects and add their property values.
Example
const obj1 = { a:12, b:8, c:17 }; const obj2 = { a:2, b:4, c:1 }; const sumObjectsByKey = (...objs) => { const res = objs.reduce((a, b) => { for (let k in b) { if (b.hasOwnProperty(k)) a[k] = (a[k] || 0) + b[k]; } return a; }, {}); return res; } console.log(sumObjectsByKey(obj1, obj2));
Output
And the output in the console will be −
{ a: 14, b: 12, c: 18 }
- Related Articles
- How to merge properties of two JavaScript Objects dynamically?
- Sort Array of objects by two properties in JavaScript
- Map multiple properties in array of objects to the same array JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- JavaScript Separate objects based on properties
- How to concatenate two JavaScript objects with plain JavaScript ?
- JavaScript Union of two objects
- Sort an array of objects by multiple properties in JavaScript
- How to access properties of an array of objects in JavaScript?
- Merge JavaScript objects with the same key value and count them
- Filter array of objects whose properties contains a value in JavaScript
- Get the total number of same objects in JavaScript
- Properties of SAP ABAP Development Objects
- Finding the group with largest elements with same digit sum in JavaScript
- Sum similar numeric values within array of objects - JavaScript

Advertisements