Skip to content

Instantly share code, notes, and snippets.

@adambene
Last active March 7, 2018 19:27
Show Gist options
  • Save adambene/3ce5c624d3437e7ab18aa72c67d23529 to your computer and use it in GitHub Desktop.
Save adambene/3ce5c624d3437e7ab18aa72c67d23529 to your computer and use it in GitHub Desktop.
Merge two arrays in JavaScript
// 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