swift - Join Multidimension Arrays -
i have multiple arrays of different sizes, instance:
let array1 = [[1, 2, 3], [1, 2, 3]] let array2 = [[1, 2], [1, 2]] let array3 = [[1, 2], [1, 2], [1, 2]]
and wanna join them final array:
let finalarray = [[1, 2, 3, 1, 2, 1, 2], [1, 2, 3, 1, 2, 1, 2], [1, 2]]
any ideia on how can achieve goal in efficient way?
try this:
var finalarray:[[int]] = [] index in 0..<max(array1.count,array2.count,array3.count) { finalarray.append([]) if index < array1.count { finalarray[index].appendcontentsof(array1[index]) } if index < array2.count { finalarray[index].appendcontentsof(array2[index]) } if index < array3.count{ finalarray[index].appendcontentsof(array3[index]) } } finalarray // [[1, 2, 3, 1, 2, 1, 2], [1, 2, 3, 1, 2, 1, 2], [1, 2]]
Comments
Post a Comment