
- 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 program to merge two objects into a single object and adds the values for same keys
We have to write a function that takes in two objects, merges them into a single object, and adds the values for same keys. This has to be done in linear time and constant space, means using at most only one loop and merging the properties in the pre-existing objects and not creating any new variable.
So, let’s write the code for this function −
Example
const obj1 = { value1: 45, value2: 33, value3: 41, value4: 4, value5: 65, value6: 5, value7: 15, }; const obj2 = { value1: 34, value3: 71, value5: 17, value7: 1, value9: 9, value11: 11, }; const mergeObjects = (obj1, obj2) => { for(key in obj1){ if(obj2[key]){ obj1[key] += obj2[key]; }; }; return; }; mergeObjects(obj1, obj2); console.log(obj1);
Output
The output in the console will be −
{ value1: 79, value2: 33, value3: 112, value4: 4, value5: 82, value6: 5, value7: 16 }
- Related Articles
- How to merge objects into a single object array with JavaScript?
- Split keys and values into separate objects - JavaScript
- How to Flatten JavaScript objects into a single-depth Object?
- Merge two objects in JavaScript ignoring undefined values
- How to merge two JavaScript objects?
- Merge object & sum a single property in JavaScript
- Merge JavaScript objects with the same key value and count them
- How to merge properties of two JavaScript Objects dynamically?
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- How to create a third object from two objects using the key values in JavaScript?
- Iterate through Object keys and manipulate the key values in JavaScript
- How to merge two different array of objects using JavaScript?
- How to convert square bracket object keys into nested object in JavaScript?
- Java program to merge two files into a third file
- Add values of matching keys in array of objects - JavaScript

Advertisements