Let suppose I have two array
let array1 = [{id: 1,name: "a"},{id: 2,name: "b"}]
let array2 = [{id: 1,name: 'c'},{id: 3,name: 'd'}]
and I want the resulting array to be like so
let result = [{id: 1,name: 'c'},{id: 2,name: 'b'},{id: 3,name: 'd'}]
So if the second array has an object and the first array also has an object with the same id then replace the first object with the second array object. Currently, I have try below code but it checks for each value to match and I want to match on id based only
const uniqueArray = this.servicesdata.filter((item, index) => {
const _item = JSON.stringify(item);
return (
index ===
this.servicesdata.findIndex(obj => {
return JSON.stringify(obj) === _item;
})
);
});
console.log(uniqueArray);