Skip to content

Latest commit

 

History

History
26 lines (20 loc) · 463 Bytes

merge-different-arrays.mdx

File metadata and controls

26 lines (20 loc) · 463 Bytes
category created tags title
Tip
2021-02-22
JavaScript
Merge different arrays

We can use the ES6 spread operator to merge different arrays into one:

const a = [1, 2, 3];
const b = [4, 5, 6];
const c = [7, 8, 9];

const final = [...a, ...b, ...c]; // [1, 2, 3, 4, 5, 6, 7, 8, 9]

The spread operator is handy if we use the push method as well:

const a = [1, 2, 3];
const b = [4, 5, 6];

a.push(...b);
a; // [1, 2, 3, 4, 5, 6]