2016-01-16 20 views
5

HoCome combinare due array come un prodotto cartesiano?

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

voglio ottenere array3 in cui tutti gli elementi di array1 con il primo (e altri) elemento di array2 ed ecc

Ad esempio:

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

capisco che Ho bisogno di usare per il ciclo ma non so come farlo.

+1

Se siete sottolineatura o lodash, una semplice [ 'zipWith'] (https://lodash.com/docs#zipWith) potrebbe funzionare: _.zipWith (array1, array2, funzione' (a, b) {return a + '' + b;}); ' –

risposta

11

È possibile utilizzare due per-loop:

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

var array3 = []; 
for (var i = 0; i < array1.length; i++) { 
    for (var j = 0; j < array2.length; j++) { 
     array3.push(array2[j] + ' ' + array1[i]); 
    } 
} 

console.log(array3); 
+0

davvero bello.Grazie – frostrock

12

È possibile utilizzare Array.prototype.forEach() per l'iterazione negli array.

Il metodo forEach() esegue una funzione fornita una volta per elemento di matrice.

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

 
array1.forEach(function (a) { 
 
    array2.forEach(function (b) { 
 
     result.push(b + ' ' + a); 
 
    }); 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

6

Un altro modo con reduce e map e concat

frammento riferiscono al @Nina Scholz

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

 
var result = array1.reduce(function (acc, cur) { 
 
    return acc.concat(array2.map(function (name) { 
 
     return name + ' ' + cur; 
 
    })); 
 
},[]); 
 

 
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

5

C'è ancora la possibilità con i loop:

var array2 = [1,2,3,4,5], 
array1 = ["one","two","three","four","five"], 
m = []; 
for(var a1 in array1){ 
    for(var a2 in array2){ 
     m.push(array1[a1]+ array2[a2]);  
    } 
} 
console.log(m); 
+3

[** Nota **: per ... in non deve essere utilizzato per iterare su una matrice in cui l'ordine dell'indice è importante .] (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for...in) – Grundy

0

Try (JS)

function myFunction(){ 
      var F = [1, 2, 3, 4,5]; 
      var S = ["one", "two", "three", "four", "five"]; 
      var Result = []; 

      var k=0; 
      for (var i = 0; i < F.length; i++) { 
       for (var j = 0; j < S.length; j++) { 
        Result[k++] = S[j] + " " + F[i]; 
       } 
      } 

      console.log(Result); 
     } 
+1

Questa è una domanda JS. L'OP potrebbe essere in grado di estrapolare il codice da questo, ma non penso che sia utile. – Andy

+0

Corretto si prega di controllare –

3

È possibile utilizzare questo metodo quando array1.length e array2.length sono uguali.

var array1 = [1, 2, 3, 4, 5]; 
 
var array2 = ["one", "two", "three", "four", "five"]; 
 
var length = array1.length; 
 
var array3 = new Array(Math.pow(length, 2)).fill(0).map((v, i) => array2[i % length] + ' ' + array1[i/length << 0]); 
 

 

 
document.body.textContent = JSON.stringify(array3);

+0

cosa succede se la matrice ha una lunghezza diversa? – Grundy

+1

@Grundy Questa domanda non lo menziona. – Lewis

+0

Sì, ma penso che dovresti aggiungere raffinatezza che funzioni per array con le stesse lunghezze – Grundy

Problemi correlati