Skip to content

Instantly share code, notes, and snippets.

@jafow
Created June 15, 2016 01:53
Show Gist options
  • Save jafow/4e358edf566bc9c6a567e83c08c87fbd to your computer and use it in GitHub Desktop.
Save jafow/4e358edf566bc9c6a567e83c08c87fbd to your computer and use it in GitHub Desktop.
divorce
/**
* divorce takes an array of nested array pairs and returns 2 one-dimensional arrays with elements from each pair, in order
* ex: divorce([[1,2], [3,4]]) --> {a1: [1,3], a2: [2,4]}
*/
const divorce = (arr, a1 = [], a2 = []) =>
!arr[0]
? { a1, a2 }
: divorce(arr.slice(1), a1.concat(arr[0][0]), a2.concat(arr[0][1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment