
- 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
Manipulating objects in array of objects in JavaScript
Suppose, we have two arrays of objects like this −
const arr1 = [ {id:'124',name:'qqq'}, {id:'589',name:'www'}, {id:'45',name:'eee'}, {id:'567',name:'rrr'} ]; const arr2 = [ {id:'124',name:'ttt'}, {id:'45',name:'yyy'} ];
We are required to write a JavaScript function that takes in two such objects. Our function should search the first array for objects with the same "id" property that is present in the second array.
Then our function should replace the "name" property of those objects with the corresponding "name" property of the second array's objects.
Therefore, for the above arrays, the output should look like −
const output = [ {id:'124',name:'ttt'}, {id:'589',name:'www'}, {id:'45',name:'yyy'}, {id:'567',name:'rrr'} ];
Example
The code for this will be −
const arr1 = [ {id:'124',name:'qqq'}, {id:'589',name:'www'}, {id:'45',name:'eee'}, {id:'567',name:'rrr'} ]; const arr2 = [ {id:'124',name:'ttt'}, {id:'45',name:'yyy'} ]; const replaceByOther = (arr1, arr2) => { for(let i = 0; i < arr1.length; i++){ const el = arr1[i]; const index = arr2.findIndex(elm => el['id'] === elm['id']); if(index === -1){ continue; }; else['name'] = arr2[index]['name']; }; }; replaceByOther(arr1, arr2); console.log(arr1);
Output
And the output in the console will be −
[ { id: '124', name: 'ttt' }, { id: '589', name: 'www' }, { id: '45', name: 'yyy' }, { id: '567', name: 'rrr' } ]
- Related Articles
- Converting array of objects to an object of objects in JavaScript
- Filtering array of objects in JavaScript
- Combine array of objects in JavaScript
- Creating an array of objects based on another array of objects JavaScript
- Loop backward in array of objects JavaScript
- Compare array of objects - JavaScript
- JavaScript - length of array objects
- Extract unique objects by attribute from an array of objects in JavaScript
- Array of objects to array of arrays in JavaScript
- Search from an array of objects via array of string to get array of objects in JavaScript
- Filter an array containing objects based on another array containing objects in JavaScript
- Convert object of objects to array in JavaScript
- Flat array of objects to tree in JavaScript
- Convert object to array of objects in JavaScript
- Update array of objects with JavaScript?

Advertisements