Skip to content
Advertisement

Push multiple elements to array

I’m trying to push multiple elements as one array, but getting an error:

JavaScript

I’m trying to do similar stuff that I’d do in ruby, I was thinking that apply is something like *.

JavaScript

Advertisement

Answer

When using most functions of objects with apply or call, the context parameter MUST be the object you are working on.

In this case, you need a.push.apply(a, [1,2]) (or more correctly Array.prototype.push.apply(a, [1,2]))

Advertisement