.map()
Anonymous contributor
Anonymous contributor3 total contributions
Anonymous contributor
Published Nov 22, 2022
Contribute to Docs
The .map()
method returns a new array containing the transformed values from calling the passed-in closure on each element of the given collection. It can be used on types that follow Swift’s Sequence
and Collection
protocols such as arrays, sets, and dictionaries.
Note: The original container is not modified, only the new array will contain the modified values.
Syntax
arrayName.map()
Example
let name = ["Gaeun", "River", "Minkyeong", "Dani"]let lowercaseNames = name.map { $0.lowercased() }print(lowercaseNames)let letterCounts = name.map { $0.count }print(letterCounts)
In the example above, each element in the name
array is lower-cased and placed into a new array named lowerCaseNames
. Then, the name
array is mapped over again and a new array is returned with the count of each element. This will output:
["gaeun", "river", "minkyeong", "dani"][5, 5, 9, 4]
All contributors
- Anonymous contributorAnonymous contributor3 total contributions
- Anonymous contributor
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.