How to update an object by another object then ignore the columns don’t exist in the object?
Let say I have 2 objects
Object A:
[
{
"title": "herry patter",
"actors": [
{
"name": "Harry",
"feature": "lighting"
},
{
"name": "Ron",
"feature": "Red"
}
]
}
]
Object B
[
{
"title": "harry potter",
"nothingHere": "bybye",
"actors": [
{
"name": "Harry Potter",
"feature": "lighting Scar"
},
{
"name": "Hermione",
"feature": "smart"
}
]
}
]
After the processing, the Object should be updated by the existing field,
but it ignore the unexpected field that is missing from the object A.
Result:
[
{
"title": "harry potter",
"actors": [
{
"name": "Harry Potter",
"feature": "lighting Scar"
},
{
"name": "Hermione",
"feature": "smart"
}
]
}
]
2
Answers
You can map the items from
a
, and for each item, reduce the keys into a new object by referencing the corresponding value based on the current index of theb
array.This approach uses deep inspection, and so it doesn’t simply replace an entire ‘actors’ array with another. It goes deep, and checks each item within the each of the ‘actors’ arrays.