Maybe a different example might help cement your knowledge?
Think about a deck of cards. All the face cards represent astronauts. Maybe your user asks to see all the astronauts who flew on the "Diamonds" mission.
What steps would you take to find the Jack, Queen, and King of diamonds in the deck of cards?
You might flip through each card, one at a time, and pull out all the diamonds. Then you might flip through the 13 diamond cards looking only for the face cards. This is how you might do it by hand.
But what @twostraws wanted to teach you is the .filter
and .map
commands for an array.
.filter
is the array method for flipping through a deck of cards.
// Paste into Playgrounds
let deckOfCards =
[("♥️", value:"👸🏾"), ("♦️", value:"3"), ("♠️", value:"👸🏾"), ("♦️", value:"5"),
("♦️", value:"🃋"), ("♦️", value:"👸🏾"), ("♦️", value:"🤴🏽")
]
// Flip through the deck of cards. First find queens, the find the diamond.
var allQueens = deckOfCards.filter { $0.value == "👸🏾" } // find all the queens in the deck.
var queenOfDiamonds = allQueens.filter { $0.0 == "♦️" } // find the queen of diamonds card.
// Note that both of these are the same type as deckOfCards. Both are arrays of tuples.
allQueens // shows three cards, queen of diamonds, hearts, and spades
queenOfDiamonds // array of just one card, the queen of diamonds
// .map() transforms the array of tuples into an array of Strings
let queenOfDiamond_card = queenOfDiamonds.map { $0.value + " " + $0.0 }
queenOfDiamond_card // This is a [String]. .map changed the tuple to a string array
This is what you did with the array of all astronauts. First you found just the three astronauts for a single mission. But you needed more than just the plain astronaut data. You also needed his ROLE within the mission. So you used the .map() feature to transform a single Astronaut object into a Crew object. All three Crew objects form a new array in your Mission object, namely
let crew = [Crew] // an array of Crew members.