
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of Two Objects with Same Properties in JavaScript
Obtaining the sum of two objects in JavaScript having the same properties Obtaining the sum of two objects in JavaScript having the same properties is a basic implementation with JavaScript objects. To do this, we are required to write a JavaScript function that takes in two such objects. The function should sum the values of identical properties into a single property.
In JavaScript, objects are data structures that allow you to store collections of key-value pairs. Sometimes, you might need to sum the values of two objects that have the same properties. This article will guide you on how to do this.
Understanding the Problem
When you have two objects with the same property names, the goal is to make a new object where each property's value is the total of the matching values from both objects.
Suppose, we have two objects like these:
const obj1 = { a:12, b:8, c:17 }; const obj2 = { a:2, b:4, c:1 };
And result object you want:
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.
Implementation Steps
To achieve this, follow the steps mentioned below:
- Define Input Objects ? Create two objects (obj1 and obj2) that contain properties with numerical values.
- Create the Function ? Define a function (such as: sumObjectsByKey) that uses the rest parameter syntax (...objs) to accept any number of objects as arguments.
- Use reduce to combine values ? Inside the function, use the reduce method to iterate over the array of objects (objs). This method will accumulate results into a single object.
- Iterate Over Each Object's Properties ? For each object (b), use a for...in loop to iterate through its properties.
- Check for Property Existence ? Use hasOwnProperty to ensure that you are only accessing direct properties of the object.
- Sum Values for Each Key ? For each property key (k), add its value from the current object (b) to the total in the result object (a). If the property isn't already in a, initialize it to 0.
- Return Result Object ? After processing all objects, return the resulting object containing summed values.
Example
The following is an example of obtaining the sum of two objects having same properties that is following the above steps:
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
{ a: 14, b: 12, c: 18 }
Conclusion
This article demonstrated a simple method of obtaining the sum of two objects having the same properties in JavaScript. In this article, we have created a simple function that will not only be used to sum two objects but also multiple objects having the same properties.