
- 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 - Merge two arrays according to id property
Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.
The array contains objects with user ids and user addresses.
The array is −
const arr1 = [ {"id":"123","name":"name 1"}, {"id":"456","name":"name 2"} ]; const arr2 = [ {"id":"123","address":"address 1"}, {"id":"456","address":"address 2"} ];
We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.
The third array should contain the user id, name, and address object of corresponding users.
Example
The code for this will be −
const arr1 = [ {"id":"123","name":"name 1"}, {"id":"456","name":"name 2"} ]; const arr2 = [ {"id":"123","address":"address 1"}, {"id":"456","address":"address 2"} ]; const mergeArrays = (arr1 = [], arr2 = []) => { let res = []; res = arr1.map(obj => { const index = arr2.findIndex(el => el["id"] == obj["id"]); const { address } = index !== -1 ? arr2[index] : {}; return { ...obj, address }; }); return res; }; console.log(mergeArrays(arr1, arr2));
Output
And the output in the console will be −
[ { id: '123', name: 'name 1', address: 'address 1' }, { id: '456', name: 'name 2', address: 'address 2' } ]
- Related Articles
- How to merge two arrays in JavaScript?
- Merge two arrays with alternating Values in JavaScript
- How to merge two arrays with objects in one in JavaScript?
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Merge two sorted arrays in Java
- Merge two sorted arrays using C++.
- Merge two sorted arrays in C#
- How to merge two object arrays of different size by key in JavaScript
- Merge two arrays using C# AddRange() method
- C# program to merge two sorted arrays into one
- How to merge two JavaScript objects?
- How to merge two arrays without duplication in android listview?
- Merge two sorted arrays in Python using heapq?
- Merge two arrays keeping original keys in PHP
- Merge arrays in column wise to another array in JavaScript

Advertisements