I am working with two arrays of objects and need to find a better solution.
array1= [{id:1,name:"samsung"},{id:2,name:"nokia"},{id:3,name:"Lg"}];
array2 = [{id:5,name:"samsung"},{id:2,name:"panasonics"},{id:7,name:"Lg"}];
The expected output should be: When the id from the first array matches the id from the second array, we want to use the name from the second array. In this case, since id 2 matches, the desired result is id:2,name: panasonics.
Output:
[{id:1,name:"samsung"},{id:2,name:"panasonics"},{id:3,name:"Lg"},{id:5,name:"samsung"},{id:7,name:"Apple"}]