Two arrays are in my possession. The task at hand is to combine both arrays while ensuring that the resulting output contains all the records from arr1 and only unique records from arr2, with the "number" field being the key.
I am seeking advice on the best approach to achieve this desired outcome. In essence, I aim to merge the arrays and extract the unique "number" values, with the result encompassing all records from arr1 and solely distinct records from arr2.
let arr1 = [
{
"number": "1234",
"Name": "test",
"data": [
{
"qty": 0,
"val": 1.11
},
{
"qty": 500,
"val": 2.92
},
{
"qty": 1000,
"val": 2.84
},
{
"qty": 1500,
"val": 2.66
}
]
},
{
"number": "6776",
"Name": "test9",
"data": [
{
"qty": 0,
"val": 2
},
{
"qty": 100,
"val": 3
},
{
"qty": 200,
"val": 4
},
{
"qty": 300,
"val": 5
}
]
},
]
let arr2 = [
{
"number": "1234",
"Name": "test",
"data": [
{
"qty": 0,
"val": 1.11
}
]
},
{
"number": "7896",
"Name": "test4",
"data": [
{
"qty": 0,
"val": 5.11
},
{
"qty": 500,
"val": 6.92
},
{
"qty": 1000,
"val": 3.84
},
{
"qty": 1500,
"val": 1.66
}
]
},
{
"number": "4567",
"Name": "test2",
"data": [
{
"qty": 0,
"val": 4.11
},
{
"qty": 500,
"val": 9.92
},
{
"qty": 1000,
"val": 5.84
},
{
"qty": 1500,
"val": 7.66
}
]
}
]
Upon merging the above two arrays, the expected output is as follows:
[
{
"number": "1234",
"Name": "test",
"data": [
{
"qty": 0,
"val": 1.11
},
{
"qty": 500,
"val": 2.92
},
{
"qty": 1000,
"val": 2.84
},
{
"qty": 1500,
"val": 2.66
}
]
},
{
"number": "6776",
"Name": "test9",
"data": [
{
"qty": 0,
"val": 2
},
{
"qty": 100,
"val": 3
},
{
"qty": 200,
"val": 4
},
{
"qty": 300,
"val": 5
}
]
},
{
"number": "7896",
"Name": "test4",
"data": [
{
"qty": 0,
"val": 5.11
},
{
"qty": 500,
"val": 6.92
},
{
"qty": 1000,
"val": 3.84
},
{
"qty": 1500,
"val": 1.66
}
]
},
{
"number": "4567",
"Name": "test2",
"data": [
{
"qty": 0,
"val": 4.11
},
{
"qty": 500,
"val": 9.92
},
{
"qty": 1000,
"val": 5.84
},
{
"qty": 1500,
"val": 7.66
}
]
}
]