JavaScript: Create a new object from the combination of two or more objects
JavaScript fundamental (ES6 Syntax): Exercise-178 with Solution
Merge Objects into New Object
Write a JavaScript program to create a new object from the combination of two or more objects.
- Use Array.prototype.reduce() combined with Object.keys() to iterate over all objects and keys.
- Use Object.prototype.hasOwnProperty() and Array.prototype.concat() to append values for keys existing in multiple objects.
Sample Solution:
JavaScript Code:
Output:
{"a":[{"x":2},{"y":4},{"z":3}],"b":[1,2,3],"c":"foo"}
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that merges two or more objects into a new object without mutating the originals.
- Write a JavaScript function that uses the spread operator to combine multiple objects into one.
- Write a JavaScript program that deep merges objects, resolving conflicts by taking the value from the last object.
- Write a JavaScript function that iterates through an array of objects and constructs a single aggregated object.
Improve this sample solution and post your code through Disqus
Previous: Write a JavaScript program to get the minimum value of an array, after mapping each element to a value using the provided function.
Next: Write a JavaScript program to compare two objects to determine if the first one contains equivalent property values to the second one, based on a provided function.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics