Home > Mobile >  How to delete objects from array by list of IDs in TypeScript
How to delete objects from array by list of IDs in TypeScript

Time:10-20

I have an array of objects that have specific IDs and another second array of objects that also have specified IDs. I want to delete objects from the first array that have the same ID as objects in the second array. How can I do it?

CodePudding user response:

Is that what you're trying to do?

let arr1 = [{
  id: 0,
  name: 'x'
}, {
  id: 1,
  name: 'y'
}, {
  id: 2,
  name: 'z'
}];

const arr2 = [{
  id: 0,
  name: 'x'
}];

arr1 = arr1.filter(element => !arr2.find(el => el.id === element.id));

console.log(arr1);

CodePudding user response:

Here is a link that seems to fit what you are trying to accomplish.

How to merge two arrays in JavaScript and de-duplicate items

  • Related