I have the following Arrays:
A = [
{
"id": "111a",
"week": 2,
"percentComplete": 50
},
{
"id": "111b",
"week": 2,
"percentComplete": 60
}
]
B = [
{
"id": "111a",
"week": 1,
"percentComplete": 20
},
{
"id": "111b",
"week": 1,
"percentComplete": 30
},
{
"id": "111c",
"week": 1,
"percentComplete": 40
}
]
Expected Output
I am trying to merge Array 'A' with array 'B' and create a new array C. Array C will have all the unique objects from 'B' and replace the ones that has common id with updated value from Array 'A'.
I tried using set function but it returns all the item, instead of returning the unique ones:
C = [...new Set([...A, ...B])]
C = [
{
"id": "111a",
"week": 1,
"percentComplete": 50
},
{
"id": "111b",
"week": 1,
"percentComplete": 60
},
{
"id": "11c",
"week": 1,
"percentComplete": 40
}
]