Why doesn't Array.push.apply work?
Solution 1:
It's Array.prototype.push
, not Array.push
Solution 2:
You can also use [].push.apply(a, b)
for shorter notation.
Solution 3:
The current version of JS allows you to unpack an array into the arguments.
var a = [1, 2, 3, 4, 5,];
var b = [6, 7, 8, 9];
a.push(...b); //[1, 2, 3, 4, 5, 6, 7, 8, 9];
Solution 4:
What is wrong with Array.prototype.concat
?
var a = [1, 2, 3, 4, 5];
var b = [6, 7, 8, 9];
a = a.concat(b); // [1, 2, 3, 4, 5, 6, 7, 8, 9];