How to merge two different array of objects using JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

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"
         }
      ]
   }
]
raja
Published on 23-Nov-2020 14:46:38
Advertisements