I have two arrays of objects. How can I combine/concat them in to one array of object.
I have tried using the concat function as well iterating through arr2 and pushing them in arr1, but I want to do in a shorter way instead.
let arr1 = [{
_id: 1,
external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f',
name: 'Francisca Rasmussen',
alias: 'Miss Coffey'
},
{ _id: 19,
external_id: '68e35e26-7b1f-46ec-a9e5-3edcbcf2aeb9',
name: 'Francis Rodrigüez',
alias: 'Mr Lea'
},
{ _id: 23,
external_id: 'e9db9277-af4a-4ca6-99e0-291c8a97623e',
name: 'Francis Bailey',
alias: 'Miss Singleton'
}];
let arr2 = [ { organizations: 'Multron', joining_key: 1 },
{ organizations: 'Bitrex', joining_key: 19 },
{ organizations: 'Enthaze', joining_key: 23 },
{ tickets: 'A Nuisance in Kiribati', joining_key: 1 },
{ tickets: 'A Nuisance in Saint Lucia', joining_key: 19 }
{ tickets: 'A Nuisance in Saint Kilda', joining_key: 19 }
]
I want to concat them which gives result as below based on the joining_key which is _id field on arr1:
[{
_id: 1,
external_id: '74341f74-9c79-49d5-9611-87ef9b6eb75f',
name: 'Francisca Rasmussen',
alias: 'Miss Coffey',
organizations: 'Multron',
tickets: 'A Nuisance in Kiribati', joining_key: 1
},
{ _id: 19,
external_id: '68e35e26-7b1f-46ec-a9e5-3edcbcf2aeb9',
name: 'Francis Rodrigüez',
alias: 'Mr Lea',
organizations: 'Bitrex',
tickets: 'A Nuisance in Saint Lucia',
tickets: 'A Nuisance in Saint Kilda'
},
{ _id: 23,
external_id: 'e9db9277-af4a-4ca6-99e0-291c8a97623e',
name: 'Francis Bailey',
alias: 'Miss Singleton',
organizations: 'Enthaze'
}]
arr1is an array and not a single object and what should happen if the array has another object.