I need to merge two arrays into a single array. I have code but it is not working as expected-- it is merging them one after another, but I need to interlock the values.
Well actually JavaScript lacks a .zip()
functor which would be very handy for exactly what you are trying to do. Lets invent it;
Array.prototype.zip = function(a){
return this.map((e,i) => typeof e === "object" || typeof a[i] === "object" ? [e,a[i]] : e+a[i]);
};
var arrays = [[1, 2, 3, 4],
["a", "b", "c", "d"],
[9, 8, 7, 6],
["z", "y", "x", "w"],
[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
],
result = arrays.reduce((p,c) => p.zip(c));
console.log(result);