Asep Bagja Priandana Asep Bagja Priandana - 2 years ago 154
iOS Question

How To Filter Array Using Element in Another Array in Swift?

I have two arrays

let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]

How to filter the first array using the second array? Actually the
can be changed dynamically, e.g,

let theFilter = ["star2"]
or maybe
let theFilter = ["star0", "star4", "star2"]

Thanks for your help :)

Answer Source

I think you're looking this:

toBeFiltered.filter { theFilter.contains($0) }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download