There are two arrays, details
and options
, each originating from different sources such as web API requests.
details: [
{ id: 'groups', option: true },
{ id: 'category', option: false }
]
options: {
groups: [
{ id: 'g1' },
{ id: 'g2' }
],
category: [
{ id: 'c1' },
{ id: 'c2' }
],
other: [
{ id: 'o1' },
{ id: 'o2' }
],
}
The goal is to merge these arrays in the following format:
combined: [
groups:
{
options:[
{ id: 'g1' },
{ id: 'g2' }
],
details: { option: true}
},
category:
{
options: [
{ id: 'c1' },
{ id: 'c2' }
],
details: { option: false}
},
]
If any id
from details
matches an id
in the options
property, it should be placed in a new array under the same property name. All details
except for the id
should go into the related details
property.
What would be the most effective method of achieving this task? Can lodash library handle such merging operation?