jeudi 29 janvier 2015

Loop through multiple arrays, combining to single array,maintaining indexes within the subarray with split string


Like on question about loop on multiple arrays, if the array two contains a split string like this:



var one = [1, 2, 3, 4];
var two = ['a', 'b', 'c,d,e,f,g', 'h'];
var three = ['U', 'V', 'W', 'X', 'Y', 'Z'];
var all = [one, two, three];
var mix = [];


how can we itenerate like answered @Andrew Clark on the code:



for (var i = 0; all.length !== 0; i++) {
var j = 0;
while (j < all.length) {
if (i >= all[j].length) {
all.splice(j, 1);
} else {
mix.push(all[j][i]);
j += 1;
}
}
}


what i would like to do is to return the split string through the arrays like:



[1, 'a', 'U', 2, 'b', 'V', 3, 'c', 'W', 4, 'h', 'X', 'Y', 'Z',1, 'a', 'U', 2, 'b', 'V', 3, 'd', 'W', 4, 'h', 'X', 'Y', 'Z',1, 'a', 'U', 2, 'b', 'V', 3, 'e', 'W', 4, 'h', 'X', 'Y', 'Z', 1, 'a', 'U', 2, 'b', 'V', 3, 'f', 'W', 4, 'h', 'X', 'Y', 'Z',1, 'a', 'U', 2, 'b', 'V', 3, 'g', 'W', 4, 'h', 'X', 'Y', 'Z']




Aucun commentaire:

Enregistrer un commentaire