array : [ {
"name": "test",
"id": "1",
"array1": [
{
"name": "Jack",
"id": "123",
}
]
},
{
"name": "test",
"id": "1",
"array1": [
{
"name": "Igor",
"id": "985",
}
]
}
]
array : [ {
"name": "test",
"id": "1",
"array1": [
{
"name": "Jack",
"id": "123",
},
{
"name": "Igor",
"id": "985",
}
]
}
]
const mapIds = {}
const filtered = array.filter(elem => {
const existingElement = mapIds[elem.id]
if (!existingElement)
mapIds[elem.id] = elem
else
existingElement.array1.push(...elem.array1)
return !existingElement
})
const mapIds = {}
const filtered = array.filter(elem =>
(!mapIds[elem.id] && (mapIds[elem.id] = elem)) || !mapIds[elem.id].array1.push(...elem.array1)
)