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

> a = [] [] > a.push.apply(null, [1,2]) TypeError: Array.prototype.push called on null or undefined 

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

>> a = [] => [] >> a.push(*[1,2]) => [1, 2] 

13 Answers

You can push multiple elements into an array in the following way

var a = []; a.push(1, 2, 3); console.log(a);
5

Now in ECMAScript2015 (a.k.a. ES6), you can use the spread operator to append multiple items at once:

var arr = [1]; var newItems = [2, 3]; arr.push(...newItems); console.log(arr);

See Kangax's ES6 compatibility table to see what browsers are compatible

2

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]))

2

You can use Array.concat:

var result = a.concat(b); 
8

If you want to add multiple items, you have to use the spread operator

a = [1,2] b = [3,4,5,6] a.push(...b) 

The output will be

a = [1,2,3,4,5,6] 
1

If you want an alternative to Array.concat in ECMAScript 2015 (a.k.a. ES6, ES2015) that, like it, does not modify the array but returns a new array you can use the spread operator like so:

var arr = [1]; var newItems = [2, 3]; var newerItems = [4, 5]; var newArr = [...arr, ...newItems, ...newerItems]; console.log(newArr);

Note this is different than the push method as the push method mutates/modifies the array.

If you want to see if certain ES2015 features work in your browser check Kangax's compatibility table.

You can also use Babel or a similar transpiler if you do not want to wait for browser support and want to use ES2015 in production.

There are many answers recommend to use: Array.prototype.push(a, b). It's nice way, BUT if you will have really big b, you will have stack overflow error (because of too many args). Be careful here.

See What is the most efficient way to concatenate N arrays? for more details.

2

Easier way is

a = [] a.push(1,2,3) 

Another way is

a = [...a, 4,5,6] 

if you want to create another array

const b = a.concat(7,8,9) 

I had the same doubt and in my case, an easier solution worked for me:

let array = [] array.push(1, 2, 4, "string", new Object()) console.log(array) // logs [ 1, 2, 4, 'string', {} ] 

Pushing multiple objects at once often depends on how are you declaring your array.

This is how I did

//declaration productList= [] as any; 

now push records

this.productList.push(obj.lenght, obj2.lenght, items); 
1

Option 1: Using concat(): It takes extra space as it returns a new array.

let array1=['a','b'] let array2=['c','d'] let array3=['e','f'] let res=array1.concat(array2,array3) 

Option 2: Using spread operator: Doesn't takes extra space and operates on the same array.

let array1=['a','b'] let array2=['c','d'] let array3=['e','f'] array1.push(...array2,...array3) 

Please use ES6 spread operator:

let a = [1,2,3]; let b = [4,5,6]; a = [...a,...b]; // [1,2,3,4,5,6] 
1
var a=[]; a.push({ name_a:"abc", b:[] }); a.b.push({ name_b:"xyz" }); 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy