3

I am trying to merge two array:

array 1 [["aaa","111"],["bbb","222"],["ccc","333"]]

array 2 [["ddd","444"],["eee","555"],["fff","666"]]

What I want to achieve is to have a single array with the values respecting the array positions such as:

merged array [["aaa","111"],["ddd","444"],["bbb","222"],["eee","555"],["ccc","333"],["fff","666"]]

How could I do this using Swift 2

2 Answers 2

7
let arr1 = [["aaa","111"],["bbb","222"],["ccc","333"]]
let arr2 = [["ddd","444"],["eee","555"],["fff","666"]]
let arr3 = arr1 + arr2
print(arr3) // [["aaa", "111"], ["bbb", "222"], ["ccc", "333"], ["ddd", "444"], ["eee", "555"], ["fff", "666"]]

in you case, with specific requirements

let arr4 = zip(arr1, arr2).reduce([]) { (var arr, p:(Array<String>, Array<String>)) -> [[String]] in
    arr.append(p.0)
    arr.append(p.1)
    return arr
}
print(arr4) // [["aaa", "111"], ["ddd", "444"], ["bbb", "222"], ["eee", "555"], ["ccc", "333"], ["fff", "666"]]

UPDATE in accordance with your needs mentioned in notes you can add the rest of values this way (please change let arr4 to var arr4 first!!!)

var i = arr4.count / 2
while i < arr1.count {
    arr4.append(arr1[i++])
}

while i < arr2.count {
    arr4.append(arr2[i++])
}
print(arr4)

this gives you

[["aaa", "111"], ["ddd", "444"], ["bbb", "222"], ["eee", "555"], ["ccc", "333"], ["fff", "666"], ["zzz", "755"]]

it should work, even though one of the arrays is empty

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, but I need to get an array like : '[["aaa","111"],["ddd","444"],["bbb","222"],["eee","555"],["ccc","333"],["fff","666"]]' - not sequential
in next version of Swift you can expect { res, p([String],[String]) in var arr = res ... }, we expecting that parameters will be let mandatory ...
when is the next version of swift out?
This however, only works if the two arrays have the same number of values. What about if one array has more values? let arr1 = [["aaa","111"],["bbb","222"],["ccc","333"],["zzz","755"]] - let arr2 = [["ddd","444"],["eee","555"],["fff","666"]]
the result should contain all the extra values at the end: print(arr4) // [["aaa", "111"], ["ddd", "444"], ["bbb", "222"], ["eee", "555"], ["ccc", "333"], ["fff", "666"],["zzz","755"],...]
|
3

use zip function

Array(zip(arr1, arr2))

@edit

as @user3441734 mentioned zip returns tupes. To fix that you can use flatMap

var a = [["aaa","111"],["bbb","222"],["ccc","333"]] 
var b = [["ddd","444"],["eee","555"],["fff","666"]]
var cos = Array(zip(a, b))
var eee = cos.flatMap { [$0.0, $0.1] }

result:

[["aaa", "111"], ["ddd", "444"], ["bbb", "222"], ["eee", "555"],["ccc", "333"], ["fff", "666"]]

short answer:

var result = zip(arr1, arr2).flatMap { [$0.0, $0.1] }

3 Comments

nice one liner, but check the result .. .[(["aaa", "111"], ["ddd", "444"]), (["bbb", "222"], ["eee", "555"]), (["ccc", "333"], ["fff", "666"])]
he needs an array ... see my answer.
perfect and elegant answer. To me this should be the accepted one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.