Skip to content

Instantly share code, notes, and snippets.

@yesvods
Created August 15, 2015 11:13
Show Gist options
  • Save yesvods/51af798dd1e7058625f4 to your computer and use it in GitHub Desktop.
Save yesvods/51af798dd1e7058625f4 to your computer and use it in GitHub Desktop.
Merge Arrays in one with ES6 Array spread
const arr1 = [1,2,3]
const arr2 = [4,5,6]
const arr3 = [...arr1, ...arr2] //arr3 ==> [1,2,3,4,5,6]
@lcervanteso
Copy link

This is so strange.. I rather use arr3= arr1.concat(arr2)

This way has a lot of performance issues

@tejassalasar
Copy link

_travelDevs

Thanks.. Its working. You make my day

@duhaime
Copy link

duhaime commented Nov 20, 2020

I'm getting an error jQuery is not defined?

@gyzamaz
Copy link

gyzamaz commented Apr 18, 2021

this es6 solution is also pretty cool
const concat = (...args) => args.flat();
concat([1, 2, 3], [4, 5], [6, 7]); //➞ [1, 2, 3, 4, 5, 6, 7]

@AishwaryaDomde
Copy link

I'm getting an error jQuery is not defined?

You need to add jQuery external link in script tag

@saurabh-sp-tripathi
Copy link

arr3 = [...arr1, ...arr2]; will fail if any of them are null.

@johnsusek
Copy link

const arr = [[1,2,3], [4,5,6]]
const merged = arr.reduce((a, b) => { a.splice(0, 0, b); return a; }, [])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment