Asep Bagja Priandana Asep Bagja Priandana - 3 months ago 25
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
theFilter
can be changed dynamically, e.g,

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


Thanks for your help :)

Answer

I think you're looking this:

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