Swift 3 filter array of objects with elements of array

One approach is to update your filter to see if any value in pets is in the petArr array:

users = users.filter { $0.pets.contains(where: { petArr.contains($0) }) }

The first $0 is from the filter and it represents each User.

The second $0 is from the first contains and it represents each pet within the pets array of the current User.


If elements inside the internal array are Equatable you can just write:

array1 = array1.filter{ $0.arrayInsideOfArray1 == array2 }

If they are not, you can make them, by adopting Equatable protocol and implementing:

func ==(lhs: YourType, rhs: YourType) -> Bool

Tags:

Arrays

Swift