Answers for "swift map filter"

5

swift filter array

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(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.
Posted by: Guest on March-05-2020

Code answers related to "Swift"

Browse Popular Code Answers by Language