Swift Language Extracting a list of names from a list of Person(s)


Example

Given a Person struct

struct Person {
    let name: String
    let birthYear: Int?
}

and an Array of Person(s)

let persons = [
    Person(name: "Walter White", birthYear: 1959),
    Person(name: "Jesse Pinkman", birthYear: 1984),
    Person(name: "Skyler White", birthYear: 1970),
    Person(name: "Saul Goodman", birthYear: nil)
]

we can retrieve an array of String containing the name property of each Person.

let names = persons.map { $0.name }
// ["Walter White", "Jesse Pinkman", "Skyler White", "Saul Goodman"]