

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to merge two different array of objects using JavaScript?
Suppose, we have two different array of objects that contains information about the questions answered by some people −
const arr1=[ { PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' }, {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];
We are required to write a function that groups this data, present in both arrays according to unique persons, i.e., one object depicting the question and choices for each unique person.
Therefore, the final output should look something like this −
const output = [{ personalID:11, qusetionNumber:1, value: 'Something' }, { personalID:12, qusetionNumber:2, value: 'whatever', choice:[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' } ] }, { personalID:13, qusetionNumber:3, value: 'anything', choice:[ { qusetionNumber: '3', chID: '114', cValue: 'yellow' } ] }, { personalID:14, qusetionNumber:4, value: 'null', choice:[ { qusetionNumber: '4', chID: '115', cValue: 'red' } ] }];
Example
The code for this will be −
const arr1=[ { PersonalID: '11', qusetionNumber: '1', value: 'Something' }, { PersonalID: '12', qusetionNumber: '2', value: 'whatever' }, { PersonalID: '13', qusetionNumber: '3', value: 'anything' }, { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[ { qusetionNumber: '2', chID: '111', cValue: 'red' }, { qusetionNumber: '2', chID: '112', cValue: 'green'}, { qusetionNumber: '2', chID: '113', cValue: 'blue' }, {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'}, {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ]; const mergeArray = (arr1 = [], arr2 = []) => { let i = -1; const copy = arr1.slice(); copy.forEach(obj => { const helper = []; arr2.forEach(obj2 => { if(obj.qusetionNumber == obj2.qusetionNumber){ i++; helper.push(arr2[i]); }; }) if(helper.length !== 0){ obj.choice = helper; }; }) return copy; }; console.log(JSON.stringify(mergeArray(arr1, arr2), undefined, 4));
Output
And the output in the console will be −
[ { "PersonalID": "11", "qusetionNumber": "1", "value": "Something" }, { "PersonalID": "12", "qusetionNumber": "2", "value": "whatever", "choice": [ { "qusetionNumber": "2", "chID": "111", "cValue": "red" }, { "qusetionNumber": "2", "chID": "112", "cValue": "green" }, { "qusetionNumber": "2", "chID": "113", "cValue": "blue" } ] }, { "PersonalID": "13", "qusetionNumber": "3", "value": "anything", "choice": [ { "qusetionNumber": "3", "choiceID": "114", "cValue": "yellow" } ] }, { "PersonalID": "14", "qusetionNumber": "4", "value": "null", "choice": [ { "qusetionNumber": "4", "choiceID": "115", "cValue": "red" } ] } ]
- Related Questions & Answers
- How to merge two JavaScript objects?
- How to merge properties of two JavaScript Objects dynamically?
- How can I merge properties of two JavaScript objects dynamically?
- Merge two objects in JavaScript ignoring undefined values
- How to merge two arrays with objects in one in JavaScript?
- How to merge two object arrays of different size by key in JavaScript
- Merge objects in array with similar key JavaScript
- How to merge objects into a single object array with JavaScript?
- Using methods of array on array of JavaScript objects?
- How can we merge two JSON objects in Java?
- How to merge two arrays in JavaScript?
- How to combine two arrays into an array of objects in JavaScript?
- Sort Array of objects by two properties in JavaScript
- JavaScript Union of two objects
- How to merge two strings alternatively in JavaScript
Advertisements