I have 2 arrays that are structured differently, I want to check allItems and replace anything that exists in newItems. I have try using map but I'm not sure how to deal with nested arrays of objects.
const allItems = [
{
'id': 1,
'text': 'old',
'child': [
{
'id': 2,
'text': 'old'
}
]
}
]
const newItems = [
{
'id': 1,
'text': 'new',
'more_info': 'abcd'
},
{
'id': 2,
'text': 'new',
'more_info': 'abcd'
}
]
I want to return:
{
'id': 1,
'text': 'new',
'more_info': 'abcd',
'child': [
{
'id': 2,
'text': 'new'
'more_info': 'abcd'
}
]
}
]
My code (this doesn't deal with the child level):
const result = allItems.map(x => {
const item = newItems.find(({ id }) => id === x.id)
return item ? item : x
})