There are two JavaScript Arrays:
let x = [
{
id: 'Abc',
children: [
{
id: 12,
name: 'john'
}, {
id: 13,
name: 'dow'
}
]
}, {
id: 'xyz',
children: [
{
id: 123,
name: 'jack'
}, {
id: 134,
name: 'abc'
}
]
}
]
let y = [
{
id: 12,
name: 'mac'
}, {
id: 13,
name: 'dow'
}, {
id: 123,
name: 'Tom'
}, {
id: 134,
name: 'abc'
}
]
The goal is to update Array x
with the data in Array y
, resulting in the following updated array:
[
{
id: 'Abc',
children: [
{
id: 12,
name: 'mac'
}, {
id: 13,
name: 'dow'
}
]
}, {
id: 'xyz',
children: [
{
id: 123,
name: 'Tom'
}, {
id: 134,
name: 'abc'
}
]
}
]
An attempt was made using this solution:
x.map((a, index)=>{
a.children.map((b, i)=>{
// console.log('update')
y.find(o => o.id === b.id) || b;
})
})
However, the result was an undefined
value. Further exploration for a solution has been unsuccessful.