
- 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
Join two objects by key in JavaScript
Suppose, we have two child and parent JSON arrays of objects like these −
const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child name', parent: { id: 4 } }]; const parent = [{ id: 1, parentName: 'The first', child: {} }, { id: 2, parentName: 'The second', child: {} }, { id: 3, parentName: 'The third', child: {} }, { id: 4, parentName: 'The fourth', child: {} }];
We are required to write a JavaScript function that takes in these two arrays.
And our function should merge the child array objects into corresponding parent objects.
Therefore, the final output should look something like this −
const output = [ { id: 1, parentName: The first, child: {} }, { id: 2, parentName: The second, child: { id: 1, name: somename, } }, { id: 3, parentName: The third, child: {} }, { id: 4, parentName: The fourth, child: { id: 2 name: some child name, } }, ];
Example
The code for this will be −
const child = [{ id: 1, name: 'somename', parent: { id: 2 }, }, { id: 2, name: 'some child name', parent: { id: 4 } }]; const parent = [{ id: 1, parentName: 'The first', child: {} }, { id: 2, parentName: 'The second', child: {} }, { id: 3, parentName: 'The third', child: {} }, { id: 4, parentName: 'The fourth', child: {} }]; const combineParentChild = (parent, child) => { const combined = []; for (let i = 0; i < parent.length; i++) { for (let j = 0; j < child.length; j++) { if (child[j].parent.id === parent[i].id) { parent[i].child.id = child[j].id; parent[i].child.name = child[j].name; break; }; }; combined.push(parent[i]) }; return combined; }; console.log(combineParentChild(parent, child));
Output
And the output in the console will be −
[ { id: 1, parentName: 'The first', child: {} }, { id: 2, parentName: 'The second', child: { id: 1, name: 'somename' } }, { id: 3, parentName: 'The third', child: {} }, { id: 4, parentName: 'The fourth', child: { id: 2, name: 'some child name' } } ]
- Related Articles
- How to group an array of objects by key in JavaScript
- How to Sort object of objects by its key value JavaScript
- Sort Array of objects by two properties in JavaScript
- Merge objects in array with similar key JavaScript
- Grouping objects based on key property in JavaScript
- Accessing nested JavaScript objects with string key
- How to perform cartesian join for two data.table objects in R?
- Twice join of two strings in JavaScript
- How to join two arrays in JavaScript?
- How to create a third object from two objects using the key values in JavaScript?
- Find specific key value in array of objects using JavaScript
- Count by unique key in JavaScript
- How to merge two object arrays of different size by key in JavaScript
- Searching objects by value in JavaScript
- Group objects by property in JavaScript

Advertisements