Given arrays arr1
, arr2
, and newArr
(for instance):
var arr1 = ["a1", "a2", "a3", "a4", "a5"]
var arr2 = ["b1", "b2", "b3", "b4", "b5"]
var newArr = ["a1", "a2", "a3", "b1", "b2", "b3", "a4", "a5", "b4", "b5"]
I am trying to combine two arrays (arr1
and arr2
) into one array (newArr
), in such a way that newArr
grabs the first (up to) three elements from arr1
, then the first (up to) three from arr2
, then the next (up to) three from arr1
, etc.
I'd initially approached this by iterating through every element of arr1
and checking to see if the index is divisible by three (for (var i = 0)
... if (i + 1 % 3 === 0)
), but now I'm thinking that this approach is untenable when arr1.length % 3 !== 0
.
Also, is there a name for this kind of merge procedure?