Imagine I've got two arrays in JavaScript:
var geoff = ['one', 'two'];
var degeoff = ['three', 'four'];
How do I merge the two arrays, resulting in an array like this?
var geoffdegeoff = ['one', 'two', 'three', 'four'];
var geoffdegeoff = geoff.concat(degeoff);
I stumbled across this and thought to add an additional way.
note: I see you want to create a third new var.
.concat is good, but you have to create a new array (unless you override the orig).
How about if you want to merge/combine array "second" into array "first".
Here is a nifty way.
// using apply
var first = ['aa','bb','cc'];
var second = ['dd','ee'];
first.push.apply(first, second);
first;
or
Array.prototype.push.apply(first, second);
first;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With