I am working on an angular application. I have two arrays as follows:
array1 = [
{
Name: "Jack",
Id: "1",
Location: "UK"
},
{
Name: "Rose",
Id: "2",
Location: "USA"
},
{
Name: "Mary",
Id: "3",
Location: "India"
}
];
array2 = [
{
Name: "Raj",
Id: "5",
Location: "UK"
},
{
Name: "John",
Id: "2",
Location: "Germany"
},
{
Name: "Maria",
Id: "3",
Location: "Canada"
}
];
I want a resultant array such that If "Id" of any element of array1 matches "Id" of any element in array2, then whole data for that particular "Id" should be replaced in array2. So my resulArray will look as follows:
resultArray = [
{
Name: "Raj",
Id: "5",
Location: "UK"
},
{
Name: "Rose",
Id: "2",
Location: "USA"
},
{
Name: "Mary",
Id: "3",
Location: "India"
}
];
So Id= 2 and Id =3 of array 1 matches in array2, so data of Id=2 and Id=3 in array2 are replaced by that of array1. How can I do it?