Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Cómo combinar 2 matrices en 1 React

Soy bastante nuevo para reaccionar. Actualmente estoy tratando de combinar 2 elementos de matrices en uno, pero me gustaría iterar sobre cada elemento. ¿Cómo puedo hacer eso?

 const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },] const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },]

Resultado esperado: ["rojo,s","rojo,m","rojo,l"],["amarillo,s","amarillo,m","amarillo,l"]...

He intentado concat() & reduce, pero el resultado que obtengo es que dos matrices se fusionan en lugar de que los elementos se fusionen por separado. ¿Puede alguien ayudar, por favor?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar el mapa para manipular con datos.

 const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },] const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },] console.log(...arr1.map(data => { return arr2.map(data2 => data.value + "," + data2.value) }))

about 4 years ago · Juan Pablo Isaza Report

0

Esto funcionó para mí, usando bucles anidados:

 const arr1 = [ { value: "red", label: "Red" },{ value: "yellow", label: "Yellow" },{value: "Blue", label: "Blue" },] const arr2 =[ { value: "s", label: "S" },{ value: "m", label: "M" },{ value: "l", label: "L" },] const newarr = [] for (i=0; i<arr1.length; i++) { tmp = [] for (j=0; j<arr2.length; j++) { tmp.push([arr1[i].value, arr2[j].value]) } newarr.push(tmp) } console.log(newarr) // [ // [ [ 'red', 's' ], [ 'red', 'm' ], [ 'red', 'l' ] ], // [ [ 'yellow', 's' ], [ 'yellow', 'm' ], [ 'yellow', 'l' ] ], // [ [ 'Blue', 's' ], [ 'Blue', 'm' ], [ 'Blue', 'l' ] ] // ]
about 4 years ago · Juan Pablo Isaza Report

0

Pruébelo (siéntase libre de cambiar las claves a .value o .label)

 function mergeArrays(arr1, arr2) { let result = []; for (let i = 0; i < arr1.length; i++) { result.push([]); for (let j = 0; j < arr2.length; j++) { result[i] = [ ...result[i], arr1[i].value + arr2[j].value ] } } }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!