Let's start with a single array, like:

Copylet raceResult = ["one", "two", "four"]

If we want to combine each element with an offset counting from 0, we can use Array.enumerated(), along with map.

Copylet numberedRaceResult = raceResult
    .enumerated()
    .map { offset, element in "\(offset). \(element)" }

for numberedResult in numberedRaceResult {
    print(numberedResult)
}

// Prints:
// 0. one
// 1. two
// 2. four

You can see that I didn't call print inside the closure passed to map. You can do this, but it kind of defeats the purpose of map (which is to create an equal-sized output array from the transformed elements of the input array), because the result would be unused. In that case, it makes more sense to just use a for loop or a call to forEach, like @Sh_Khan showed.

To handle a nested array, it's much the same. We can use the same logic as for one array, but apply it to each sub-array.

Copylet raceResults = [
    ["one", "two", "four"],
    ["two", "one", "five", "six"],
    ["two", "one", "four", "ten"],
    ["one", "two", "four"],
]

let numberedRaceResults = raceResults
    .enumerated()
    .flatMap { outterOffset, raceResult in
        raceResult
            .enumerated()
            .map { innerOffset, element in "\(outterOffset).\(innerOffset). \(element)" }
    }

for numberedResult in numberedRaceResults {
    print(numberedResult)
}

// Prints:
// 0.0. one
// 0.1. two
// 0.2. four
// 1.0. two
// 1.1. one
// 1.2. five
// 1.3. six
// 2.0. two
// 2.1. one
// 2.2. four
// 2.3. ten
// 3.0. one
// 3.1. two
// 3.2. four

You'll notice that I used flatMap on the outter array, instead of a simple map. You can change it back and forth and compare the result. In short, flatMap gives you a single flat array of string results, rather than an array of sub-arrays of strings.

Answer from Alexander on Stack Overflow
🌐
Apple Developer
developer.apple.com › documentation › swift › sequence › map(_:)
map(_:) | Apple Developer Documentation
Returns an array containing the results of mapping the given closure over the sequence’s elements.
🌐
Hacking with Swift
hackingwithswift.com › example-code › language › how-to-use-map-to-transform-an-array
How to use map() to transform an array - free Swift example code and tips
May 28, 2019 - For example, you could convert an array of strings to be uppercase: let strings = ["John", "Paul", "George", "Ringo"] let uppercased = strings.map { $0.uppercased() } SPONSORED Join us for deep dives, hands-on workshops, and world-class speakers at try! Swift Tokyo on April 12th-14th – come and see why we're the world's largest Swift community event!
Top answer
1 of 2
1

Let's start with a single array, like:

Copylet raceResult = ["one", "two", "four"]

If we want to combine each element with an offset counting from 0, we can use Array.enumerated(), along with map.

Copylet numberedRaceResult = raceResult
    .enumerated()
    .map { offset, element in "\(offset). \(element)" }

for numberedResult in numberedRaceResult {
    print(numberedResult)
}

// Prints:
// 0. one
// 1. two
// 2. four

You can see that I didn't call print inside the closure passed to map. You can do this, but it kind of defeats the purpose of map (which is to create an equal-sized output array from the transformed elements of the input array), because the result would be unused. In that case, it makes more sense to just use a for loop or a call to forEach, like @Sh_Khan showed.

To handle a nested array, it's much the same. We can use the same logic as for one array, but apply it to each sub-array.

Copylet raceResults = [
    ["one", "two", "four"],
    ["two", "one", "five", "six"],
    ["two", "one", "four", "ten"],
    ["one", "two", "four"],
]

let numberedRaceResults = raceResults
    .enumerated()
    .flatMap { outterOffset, raceResult in
        raceResult
            .enumerated()
            .map { innerOffset, element in "\(outterOffset).\(innerOffset). \(element)" }
    }

for numberedResult in numberedRaceResults {
    print(numberedResult)
}

// Prints:
// 0.0. one
// 0.1. two
// 0.2. four
// 1.0. two
// 1.1. one
// 1.2. five
// 1.3. six
// 2.0. two
// 2.1. one
// 2.2. four
// 2.3. ten
// 3.0. one
// 3.1. two
// 3.2. four

You'll notice that I used flatMap on the outter array, instead of a simple map. You can change it back and forth and compare the result. In short, flatMap gives you a single flat array of string results, rather than an array of sub-arrays of strings.

2 of 2
1

Map is used to convert one bunch of type T into things of some other type, X. Like map these Ints to String?s. You should not use map for side-effects, like printing the values, or updating a database etc. It should be a pure function that takes an input and returns an output. "Map these A's into B's". Pure meaning the value of the function only depends on the input, nothing else like the current state of the world, and doesn't change the world either (like printing to the console), so for example, map these int's by the function that adds 2 to them.

In your example:

Copyvar raceResults = [["one","two","four"],["two","one","five","six"],["two","one","four","ten"],["one","two","four"]]

You have an array of "arrays of strings".

You can map that to an array of so long as you have a function that takes "array of string" and turns that into "something else"

Here you map it with the Identity function, the function that just returns its input, which is going to take an array of strings as input and return the exact same array of strings as output:

Copy   raceResults.map {
       return $0 // Returns first array 
   }

This does nothing, and the result is the exact same thing as raceResults.

If you want to iterate over all these elements then the function flatMap is handy:

CopyraceResults.flatMap { $0 }.forEach { print($0) }

flatMap is flatten, then map. Flattening an array of arrays is to return an array with all the things 'flattened' one level, so [[1, 2, 3], [4, 5, 6]] -> [1, 2, 3, 4, 5, 6], but the definition of what to flatten means depends on the type of container, for example flatMap on Optional means something else to flatMap on Array.

🌐
Medium
medium.com › @danielbanales › using-the-map-function-in-swift-to-transform-collections-69f34b0fc943
Using the Map Function in Swift to Transform Collections | by Daniel BR | Medium
January 10, 2023 - You probably don’t like the fact are optional and prefer to ignore the nil values; Swift already provides us with a standard way to achieve this, using the compactMap method. This method allows you to create an array, that discards nil values; thus, the type of the elements would never be optional. You can also chain methods to make more complex operations. In the above example, first, we convert the Strings to Integers, using compactMap to discard any possible nil values, and then we use map to multiply each element by 2.
🌐
Swift by Sundell
swiftbysundell.com › basics › map-flatmap-and-compactmap
Map, FlatMap and CompactMap | Swift by Sundell
July 17, 2019 - We could also use plain map, which requires us to produce a non-optional value, for example by using a default: func convertToInt(_ string: String?) -> Int? { return string.map { Int($0) ?? 0 } } Swift’s various map functions are great to keep in mind when we need to transform sequences, or optionals, into a new form.
🌐
Cocoa Casts
cocoacasts.com › swift-essentials-1-how-to-use-swift-map-to-transforms-arrays-sets-and-dictionaries
How to Use Swift Map to Transform Arrays, Sets, and ...
import Foundation let countries = [ "US": "United States", "BE": "Belgium", "CN": "China" ] let ints = countries.mapValues { $0.count } print(ints) What Are the Benefits of the Final Keyword in Swift
🌐
Programiz
programiz.com › swift-programming › library › array › map
Swift Array map() (With Examples)
The map() method transforms the array by applying the same operation to each element in the array. Example var numbers = [1, 2, 3, 4] // add 2 to each element var result = numbers.map({ $0 + 2}) print(result) // Output: [3, 4, 5, 6] map() Syntax The syntax of the map() method is:
Find elsewhere
🌐
Waldo
waldo.com › home › learn › mobile › understanding higher-order functions in swift
Understanding map, filter, reduce in Swift
January 27, 2026 - There are many higher order functions ... used to take a value of one type, and transform it into another value. For example, we could transform an array of [String] to [URL] as follows:...
🌐
Bugfender
bugfender.com › blog › swift-arrays
Swift Arrays: Map, Filter, Reduce & Sort Explained | Bugfender
November 7, 2025 - They accept a transformative function as their argument and they return the transformed argument. To take a very simple example, let’s map an array of Ints to an array of Strings:
🌐
Use Your Loaf
useyourloaf.com › blog › swift-guide-to-map-filter-reduce
Swift Guide to Map Filter Reduce
May 1, 2018 - let squares4 = values.map {value in value * value} The in keyword separates the argument from the body of the closure. If you prefer you can go one step further and use the numbered arguments shorthand: ... The type of the results is not limited ...
🌐
Sarunw
sarunw.com › posts › different-ways-to-map-dictionary-in-swift
Different ways to map over Dictionary in Swift | Sarunw
September 11, 2023 - We provide a transform closure that accepts a value of the dictionary and returns a transformed value. func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> Dictionary<Key, T> In this example...
🌐
Medium
mehrdad-ahmadian.medium.com › understanding-map-flatmap-and-compactmap-in-swift-eacafc38fb61
Understanding map, flatMap, and compactMap in Swift | by Mehrdad Ahmadian | Medium
December 24, 2023 - Swift, Apple’s powerful and intuitive programming language, has a rich set of features that make it a favorite among iOS and macOS developers. Among these features are three higher-order functions that are essential for efficient array manipulation: map, flatMap, and compactMap. In this article, we'll dive into each of these functions, explore their uses, and see them in action with practical examples...
🌐
DhiWise
dhiwise.com › post › swift-map-array-simplifying-transformations-in-swift
Enhance Your Development Workflow with Swift Map Array
June 18, 2024 - For instance, consider the example of transforming an array of Ints into a String. Swift’s map function creates an array of transformed elements in a way that each integer converts into a string.
🌐
Donny Wals
donnywals.com › when-to-use-map-flatmap-and-compactmap
When to use map, flatMap and compactMap in Swift
July 7, 2025 - The slightly more complicated sibling of map is called flatMap. You use this flavor of map when you have a sequence of sequences, for instance, an array of arrays, that you want to "flatten". An example of this is removing nested arrays so you end up with one big array.
🌐
Swift Forums
forums.swift.org › using swift
How to do a join in Swift using the map tool? - Using Swift - Swift Forums
November 1, 2023 - I have two arrays of class objects having a common value in each object. How do I do a join that would be the same as an SQL join? For example. Class AM_Template has the following public variables. class AM_Template { public var SystemID: String = "" var ULSFileNo: String = "" var CallSign: String = "" var Class: String = "" } First, the class object AM_line is created using the template and is var AM_line = AM_Template() Next, two arrays are created with each record having the for...
🌐
Appy Pie Vibe
appypievibe.ai › home › app development › map, reduce and filter in swift
Map, Reduce and Filter in Swift – Appy Pie Vibe
November 11, 2020 - In Swift you use map(), reduce() and filter() to loop over collections like arrays and dictionaries, without using a for-loop. The map, reduce and filter functions come from the realm of functional programming (FP). They’re called higher-order functions, because they take functions as input. You’re applying a function to an array, for example...
🌐
Better Programming
betterprogramming.pub › map-compactmap-and-flatmap-in-swift-4ea4f1a39cbd
Map, CompactMap, and FlatMap in Swift
February 17, 2022 - Please note one thing in the latest example. The field array is of type [String?] while the items array is of type [String] so the returning type of compactMap is never an optional. Wow, amazing! The last type of map is a function that, as the name says, flattens an array of arrays.
🌐
Hacking with Swift
hackingwithswift.com › plus › functional-programming › transforming-data-with-map
Transforming data with map() – Hacking with Swift+
May 31, 2021 - Swift actually has another function ... and map() without any further work from us: we can use key path expressions as functions. This was introduced in Swift 5.2, and it means if you can write a key path such as \X.y then you can use that in place of functions that expect an X and return y. For example, \String.count ...
🌐
DhiWise
dhiwise.com › post › exploring-the-intricacies-of-swift-map-dictionary
The Ultimate Swift Map Dictionary Tutorial for Beginners
July 10, 2024 - In the above example, the Swift dictionary map function transforms both the key and value of each element in the input dictionary by mapping the value to value+1 while keeping the same keys.
🌐
Swift.org
docs.swift.org › swift-book › documentation › the-swift-programming-language › collectiontypes
Documentation
This document is made available under a Creative Commons Attribution 4.0 International (CC BY 4.0) License · Swift and the Swift logo are trademarks of Apple Inc