JavaScript in-place concat
In JavaScript, a lot of times you want to combine two arrays. Since concat
doesn’t work in-place, you end up with something like:
var a = [1, 2, 3]; var b = [4, 5, 6]; a = a.concat(b); // [1, 2, 3, 4, 5, 6]
But since push
can take multiple arguments, we can use apply
to do a little better:
var a = [1, 2, 3]; var b = [4, 5, 6]; a.push.apply(a, b); // 6 a; // [1, 2, 3, 4, 5, 6]