I need to compare two arrays and replace values if id field is matched between these arrays. Below are the two sample arrays:
array1 = [
{
"Id": "test1",
"Date": "2021-11-05T12:53:00.000Z",
"name": "data"
},
{
"Id": "test2",
"Date": "2021-11-05T12:53:00.000Z",
"name": "data"
}
]
array2 = = [
{
"Id": "test1",
"Date": "2021-11-05T12:53:00.000Z",
"fname": "david",
"lname": "john"
},
{
"Id": "test4",
"Date": "2021-11-05T12:53:00.000Z",
"fname": "Chris",
"lname": "Smith"
}
]
In these two arrays if Id is same I need to replace "fname" from second array to "name" from first array. And if id is not same I'll print the "fname" from second array.
logic is similar like this:
if array1.id = array2.id then replace array2.fname with array1.name else keep array2.fname. In the above example since test1 is matched I should get below output:
[
{
"Id": "test1",
"Date": "2021-11-05T12:53:00.000Z",
"fname": "data",
"lname": "john"
},
{
"Id": "test4",
"Date": "2021-11-05T12:53:00.000Z",
"fname": "Chris",
"lname": "Smith"
}
]