Sunday, 11 March 2018

zip two arrays Javascript




I have two arrays in javascript:



var array1 = ["a","b","c"];

var array2 = ["e","f","g"];


And I want the resulting array to be like this:



array3 = ["a","e","b","f","c","g"];


Any way to do this?


Answer




Will a straightforward loop do it?



array3 = new Array();

for(var i = 0; i < array1.length; i++)
{
array3.push(array1[i]);
array3.push(array2[i]);
}


No comments:

Post a Comment

casting - Why wasn&#39;t Tobey Maguire in The Amazing Spider-Man? - Movies &amp; TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...