Last active
March 7, 2018 19:27
-
-
Save adambene/3ce5c624d3437e7ab18aa72c67d23529 to your computer and use it in GitHub Desktop.
Merge two arrays in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// merge two arrays a and b | |
const merge = (a, b) => a.reduce( | |
(acc, curr, i) => [ | |
...acc, // copy all previous elements | |
curr, // current element of a | |
b[i] // current element of b | |
], | |
[] // initial value | |
); | |
merge([1, 3], [2, 4]); // [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment