I am facing an issue where I have multiple objects with the same properties and want to merge them based on a common key-value pair at the first level. Although I know about using the spread operator like this:
const obj3 = {...obj1, ...obj2}
The problem arises when there are arrays inside the objects that end up getting overwritten instead of being merged seamlessly.
{
"id": 1,
"name": "firstLevel",
"visible": true,
"subCategories": [
{
"id": 2,
"name": "secondLevel",
"visible": true,
"skills": [
{
"name": "foo",
"id": 5,
"visible": true
}
]
}
]
}
{
"id": 1,
"name": "firstLevel",
"visible": true,
"subCategories": [
{
"id": 2,
"name": "secondLevel",
"visible": true,
"skills": [
{
"name": "bar",
"id": 1,
"visible": true
}
]
}
]
}
My desired outcome is for the objects to combine in such a way that the arrays within them get merged, resulting in:
{
"id": 1,
"name": "firstLevel",
"visible": true,
"subCategories": [
{
"id": 2,
"name": "secondLevel",
"visible": true,
"skills": [
{
"name": "foo",
"id": 5,
"visible": true
},
{
"name": "bar",
"id": 1,
"visible": true
}
]
}
]
}