0

I have following data:

let array1 = [[1], [2], [3]]

And I want to make it vector:

let result = [1, 2, 3]

Solution off the top of my head:

var result = [Int]()
for arrayOfArray in array1 {
  for value in arrayOfArray {
    result(value)
  }
}

Is there more elegant way to do this ?

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Vlad Z.
  • 3,401
  • 3
  • 32
  • 60

1 Answers1

4

You can use flatMap for that.

let array1 = [[1], [2], [3]]
let result = array1.flatMap { $0 } 

Output

[1, 2, 3]

Check Apple Documentation on flatMap for more details.

Nirav D
  • 71,513
  • 12
  • 161
  • 183