let array = [["a"], ["b", "c"], ["d", "e", "f"]]
let result = zip(array, array.indices).flatMap { subarray, index in
subarray.map { ($0, index) }
}
result:
[("a", 0), ("b", 1), ("c", 1), ("d", 2), ("e", 2), ("f", 2)]
I used zip(array, array.indices)instead array.enumerated()because you specifically asked for a tuple with an array index - enumerated()created tuples with zero integer offsets. If your source collection is an array, that doesn't matter, but other collections (like ArraySlice) will behave differently.
source
share