Check this out. This is more dynamic implementation with no hard-coding of assumptions.
function prepareObjectOfArrays(myArrays){
var myObj = [];
var maxArrLength = calculateMaxLength(myArrays);
for(var i=0; i<maxArrLength; i++){
var tempArr = [];
for(var j=0; j<myArrays.length; j++){
tempArr.push(myArrays[j][i]);
}
myObj.push(tempArr);
}
console.log(myObj);
}
function calculateMaxLength(myArrays){
var maxLength = 0;
for(var j=0; j<myArrays.length; j++){
var tempLength = myArrays[j].length;
if(maxLength < tempLength){
maxLength = tempLength;
}
}
return maxLength;
}
Now invoke as below:
var array1= ['ab','bc'];
var array2= [12,23];
var array3= ['vw','wx'];
prepareObjectOfArrays([array1, array2, array3]);
As I said, this is more dynamic implementation with no hard-coding of assumptions, and will even work with more type of inputs. There could be more corner cases but this is almost fully dynamic. Check with below inputs:
var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34];
var array3= ['vw','wx','yz'];
var array1= ['ab','bc','de', 'er'];
var array2= [12,23,34,45,56,78];
var array3= ['vw','wx','yz'];