I've been tackling a project in Angular where I have two arrays set up like this:
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'm looking to create a new array where if the "Id" of any element in array1 matches an "Id" from array2, then the data for that specific "Id" should be replaced in array2. The resulting array would look like this:
resultArray = [
{
Name: "Raj",
Id: "5",
Location: "UK"
},
{
Name: "Rose",
Id: "2",
Location: "USA"
},
{
Name: "Mary",
Id: "3",
Location: "India"
}
];
In this case, the Id values 2 and 3 from array1 matched with those in array2, so the corresponding data in array2 was replaced by that of array1. Any ideas on how I can achieve this?