array1 = array1.filter{ $0.arrayInsideOfArray1.contains(array2[0]) }
Code above works but I'm trying to check if all the elements of $0.arrayInsideOfArray1 match with all the elements of array2 not just [0]
Example:
struct User {
var name: String!
var age: Int!
var hasPet: Bool!
var pets: [String]!
}
var users: [User] = []
users.append(User(name: "testUset", age: 43, hasPet: true, pets: ["cat", "dog", "rabbit"]))
users.append(User(name: "testUset1", age: 36, hasPet: true, pets:["rabbit"]))
users.append(User(name: "testUset2", age: 65, hasPet: true, pets:["Guinea pigs", "Rats"]))
let petArr = ["cat", "dog", "rabbit"]
users = users.filter { $0.pets.contains(petArr[0]) }
What I want is any user that has any pet listed in the petArr!
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With