Get first element of array of pairs (Swift)

后端 未结 1 1868
一生所求
一生所求 2020-12-11 22:07

I have this array:

let arrayOfPairs = [
    (8, 1),
    (6, 4),
    (2, 3),
]

I want this array:

let arrayOfSingleValues =          


        
相关标签:
1条回答
  • 2020-12-11 22:46

    You can use map to get just the first element of your tuple as follow:

    let arrayOfPairs = [(8, 1), (6, 4), (2, 3)]
    let arrayOfSingleValues = arrayOfPairs.map { $0.0 }
    
    print(arrayOfSingleValues)   // [8, 6, 2]
    
    0 讨论(0)
提交回复
热议问题