var array1 = ["Pepsi", "Coke", "Juice", "Water"], array2 = ["35", "17", "21", "99"], result = [], i = -1; while ( array1[++i] ) { result.push( [ array1[i], array2[i] ] ); }
As written, this solution assumes that you will only use strings. As @ ajax333221 noted in the comments below, this will cause problems if you include boolean
or int
values ββin this solution. Thus, I would like to suggest an improvement that will achieve your goals without disabling complex values ββand types:
var array1 = [false, 0, "Juice", -1], array2 = ["35", "17", "21", "99"], result = []; for ( var i = 0; i < array1.length; i++ ) { result.push( [ array1[i], array2[i] ] ); }
source share