Efficient way to delete an object if it exists in one array but not in the other?

I have an array of objects where each object has an id and name
I also have another array of objects where each object has text and their own different id

How can I go through the first array and then check if an element from the second array is missing and then remove that element from the first array?
I have to compare the name field of the first array to the text field of the second.

so far I tried to do :

for(let obj1 of array1){
      let present = false;
      for(let obj2 of array2){
        if(obj1.name === obj2.text)
        present = true;
      }
     if(present === false){
     //how to delete the obj1 ?
    }

  }

But I’m wondering how to delete the object now from array1 or if there is a more efficient way of doing this?

38 thoughts on “Efficient way to delete an object if it exists in one array but not in the other?”

Leave a Comment