How can I run through three separate arrays in the same for loop?

前端 未结 6 1310
遇见更好的自我
遇见更好的自我 2020-12-06 13:38

I have three arrays I am trying to run through and I want to use the values from all three arrays in one function. This might sound confusing but here is what I have:

<
6条回答
  •  悲哀的现实
    2020-12-06 14:33

    You can cast the enumerator as an array and use functional methods to map the result to what you want.

    var Name = ["a", "b", "c"]
    var Age = [1, 2, 3]
    var Gender = ["m", "f", "m"]
    
    let results = Array(Name.enumerated())
        .map {($0.element, Age[$0.index], Gender[$0.index])}
    

提交回复
热议问题