
- 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
How to merge two object arrays of different size by key in JavaScript
Suppose, we have an object like this −
const obj = { "part1": [{"id": 1, "a": 50},{"id": 2, "a": 55},{"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] };
We are required to write a JavaScript function that takes in one such object. The function should merge part1 and part2 of the object to form an array of objects like this −
const output = [ {"id": 1, "a": 50, "b": 40}, {"id": 2, "a": 55}, {"id": 3, "b": 45}, {"id": 4, "a": 100, "b": 110} ];
Example
The code for this will be −
const obj = { "part1": [{"id": 1, "a": 50},{"id": 2, "a": 55},{"id": 4, "a": 100}], "part2":[{"id": 1, "b": 40}, {"id": 3, "b": 45}, {"id": 4, "b": 110}] }; const mergeObject = (obj = {}) => { let result = []; result = Object.keys(obj).reduce(function (hash) { return function (r, k) { obj[k].forEach(function (o) { if (!hash[o.id]) { hash[o.id] = {}; r.push(hash[o.id]); } Object.keys(o).forEach(function (l) { hash[o.id][l] = o[l]; }); }); return r; }; }(Object.create(null)), []).sort((a, b) => { return a['id'] − b['id']; }); return result; }; console.log(mergeObject(obj));
Output
And the output in the console will be −
[ { id: 1, a: 50, b: 40 }, { id: 2, a: 55 }, { id: 3, b: 45 }, { id: 4, a: 100, b: 110 } ]
- Related Articles
- How to merge two arrays in JavaScript?
- How to merge two different array of objects using JavaScript?
- How to merge two arrays with objects in one in JavaScript?
- JavaScript - Merge two arrays according to id property
- Merge two arrays with alternating Values in JavaScript
- Combine two different arrays in JavaScript
- How to merge an array with an object where values are arrays - JavaScript
- Merge k sorted arrays of different sizes in C++
- How to Sort object of objects by its key value JavaScript
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- How to merge two arrays without duplication in android listview?
- How to compare two arrays when the key is a string - JavaScript
- Merge two sorted arrays in Java
- Merge two sorted arrays in C#
- JavaScript - Set object key by variable

Advertisements