0
votes

Comment connecter un élément d'un tableau avec un autre élément d'une matrice

J'ai deux array: xxx

Comment puis-je obtenir le résultat suivant? xxx


8 Réponses :


5
votes

let array1 = ["aaa", "bbb"];
let array2 = ["f1", "f2", "f3"];

const newArray = [];

array1.forEach(item1 => {
	array2.forEach(item2 => {
		newArray.push(item1 + " " + item2)
	})
})

console.log(newArray);


0 commentaires

2
votes

Voici une solution utilisant Array.pototype.flaTmap () code> et array.pototype.map () code> :

p>

const array1 = ["aaa","bbb"];
const array2 = ["f1","f2","f3"];

const result = array1.flatMap(v1 => array2.map(v2 => `${v1} ${v2}`));

console.log(result);


0 commentaires

0
votes
'use strict';
let array1 = ["aaa","bbb"];
let array2 = ["f1","f2","f3"];

let res = [];

array1.forEach( a1 => array2.forEach(a2 => res.push([`${a1} ${a2}`])) );

0 commentaires

0
votes
let array1 = ["aaa","bbb"];
let array2 = ["f1","f2","f3"];
let array3 = []
for (ele1 of array1) {
    for (ele2 of array2) {
        array3.push(ele1 + ' ' + ele2);
    } 
}

0 commentaires

0
votes

Vous pouvez utiliser Réduire () code> et mappe () code> pour obtenir le résultat requis en utilisant avec Spread_Syntax .

Vérifiez ci-dessous CODE SNIPPET: P>

P>

let array1 = ["aaa","bbb"],
    array2 = ["f1","f2","f3"];
    
 let result = array1.reduce((r,v)=>[...r,array2.map(m=>`${v} ${m}`).join(',')],[])

console.log(result.join(','))
 


0 commentaires

0
votes

avec boucle xxx

avec pour chaque xxx

jsfiddle violon


0 commentaires

1
votes
let array1 = ["aaa", "bbb" ];
let array2 = ["f1","f2","f3"];
let response=[];
array1.forEach( ele1 => {
 array2.forEach( ele2=> {
   response.push( ele1 +' '+ ele2 );
 })
});
console.log( response );

0 commentaires

0
votes

Vous pouvez utiliser Array.Prototype.reduce ( ) combiné avec Array.Prototype.map () , Spread Syntaxe a > et Modèle littéraux

code:

p>

const array1 = ["aaa", "bbb"];
const array2 = ["f1", "f2", "f3"];

const result = array1.reduce((a, c) => [...a, ...array2.map(f => `${c} ${f}`)], []);

console.log(result);


0 commentaires