Home > other >  How do I filter an array of structures' properties with a keyword?
How do I filter an array of structures' properties with a keyword?

Time:11-17

If I have an array of strings, the formula is simple, it's something like:

let filteredArray = stringArray.filter({ $0.contains(filterWord)})

However, if I have an array of structures each with a property called "title", how do I filter and create an array of these structures with a keyword?

struct Item: Codable {
var title: String = ""
}

Thanks in advance.

CodePudding user response:

Since $0 is the current item you’re looking at just add the property you want to filter:

let filteredArray = stringArray.filter({ $0.title.contains(filterWord)})
  • Related