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

SNos
  • 3,430
  • 5
  • 42
  • 92

2 Answers2

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

user3441734
  • 16,722
  • 2
  • 40
  • 59
  • Thank you, but I need to get an array like : '[["aaa","111"],["ddd","444"],["bbb","222"],["eee","555"],["ccc","333"],["fff","666"]]' - not sequential – SNos Jan 09 '16 at 17:29
  • in next version of Swift you can expect { res, p([String],[String]) in var arr = res ... }, we expecting that parameters will be let mandatory ... – user3441734 Jan 09 '16 at 17:47
  • when is the next version of swift out? – SNos Jan 09 '16 at 17:48
  • 1
    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"]]` – SNos Jan 09 '16 at 18:10
  • 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"],...]` – SNos Jan 09 '16 at 18:28
  • @SNos see the update and check that in Swift3.0 probably i++ you have to replace with i += 1 – user3441734 Jan 09 '16 at 19:10
  • Thanks a lot.. is working ... I have tried with an empty array 'let arr1 = [[String]]()' and its working fine – SNos Jan 10 '16 at 13:41
  • I have tried with 3 arrays but I get `Extra argument in call` – SNos Jan 10 '16 at 13:44
  • It seems that you need to develop a homework. Are you sure that stackoverflow is the right place where to specify a task which someone will draw up for you? If you really have some trouble, show us your code and ask the community where you did the mistake ... or hire somebody. – user3441734 Jan 10 '16 at 14:08
  • Yes I know just asking.. I have done it anyway using `arr4` and merge it with your method to another array. Just wanted to know if there was a quick way. But thanks anyway! – SNos Jan 10 '16 at 14:11
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] }
Tomasz Pikć
  • 753
  • 5
  • 21