Asep Bagja Priandana Asep Bagja Priandana - 5 months ago 35
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 :)


I think you're looking this:

toBeFiltered.filter { theFilter.contains($0) }