Sunday, March 11, 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

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...