Combining JavaScript Array

Combining JavaScript Array

There are Several method for combine the array using Javascript.

1. concat(..);

The most common approch is

var a = [1,2,3];
var b = [4,5,6];
var a = a.concat(b);

concatinating a and b create the new array.

2. Looped insertion

var a= [1,2,3];
var b = [4,5,6];
for(var i=0;i< b.lenght; i++){ 
a.push(b[i]);
}

The above loop b push in to the a. now a has the result of the both original a and b array.

3. fuctional tricks.

//b into a
var a= [1,2,3];
var b = [4,5,6];
a.push.apply(a,b);
//a into b
var a= [1,2,3];
var b = [4,5,6];
a.unshift(b,a);

4. spread operator

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

0 Comments

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed

>