Most intelligent way of filtering between two arrays filled with objects

I have 2 different arrays filled with objects which have properties

Let’s name them arrays A and B

I want array A to have all the items B has and removing all items A has that B does not have.

Essentially is B has an item A already has, it does nothing. If B has an item A does not have, it adds it. And if A has an item B does not have, it removes it.

And finallly after all that I get the final result of array A

What’s the best way to do this?

Are you asking how to compare two objects or are you just confused about the algorithm that would help you identify which objects should be transferred from a to b?

If A has all the items in B, and none of the items that are not in B, then you can simply make A a copy of B, unless you want to preserve the original order of the items in A.

From your description (albeit I don’t think it’s actually what you meant):

function example(a, b) {
  return b;
}

Your spec says want A to be B

1 Like

That’s basically an intersection, right? An array of all the things that both arrays have in common?

Or maybe I was too rash. [Edit: yeah, now I’m seeing what Dan is saying.] If you have this:

const arrA = [1, 2, 3]
const arrB = [2, 3, 4]

const arrC = myFunc(arrA, arrB)

What do you expect to be in arrC?