javascript - How to combine two arrays as a cartesian product? -


i have

array1 = [1,2,3,4,5]; array2 = ["one","two","three","four","five"]; 

i want array3 elements of array1 first (and others) element of array2 , etc.

for example:

array3 = ["one 1", "two 1", "three 1", "four 1", "five 1", "one 2", "two 2", "three 2", "four 2", "five 2"...] 

i understand need use loop don't know how it.

you can use 2 for-loops:

var array1 = [1,2,3,4,5]; var array2 = ["one","two","three","four","five"];  var array3 = []; (var = 0; < array1.length; i++) {     (var j = 0; j < array2.length; j++) {         array3.push(array2[j] + ' ' + array1[i]);     } }  console.log(array3); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -