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

194
Views
¿Cómo puedo insertar cada uno de los elementos de una matriz cada otro elemento en otra matriz?

Tengo dos matrices.

let a = [1, 3, 5, 7] let b = [2, 4, 6, 8]

Quiero el resultado: a = [1, 2, 3, 4, 5, 6, 7, 8]

¿Cómo puedo insertar cada uno de los elementos de la matriz B en todos los demás elementos de la matriz A?

Intenté usar splice en un bucle for, pero la longitud de la matriz A cambia, por lo que no puedo hacer que funcione.

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

0

Puede crear una nueva matriz, recorrer a y empujar el elemento actual y el elemento en b en el mismo índice:

 let a = [1, 3, 5, 7]; let b = [2, 4, 6, 8]; let res = [] a.forEach((e,i) => res.push(e, b[i])) console.log(res)

Alternativamente, puede usar Array.map y Array.flat :

 let a = [1, 3, 5, 7]; let b = [2, 4, 6, 8]; let res = a.map((e,i) => [e, b[i]]).flat() console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

Si las matrices tienen la misma longitud, puede usar un mapa plano para evitar la mutación de la matriz original.

 const a = [1, 3, 5, 7]; const b = [2, 4, 6, 8]; const res = b.flatMap((elem, index) => [a[index], elem]); console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

Puedes probar:

 let a = [1, 3, 5, 7]; let b = [2, 4, 6, 8] let newArray = [...a, ...b] console.log(newArray) // [1, 3, 5, 7, 2, 4, 6, 8]

Si desea ordenar solo

 let a = [1, 3, 5, 7]; let b = [2, 4, 6, 8] let newArray = [...a, ...b].sort((a, b) => a - b) console.log(newArray) // [1, 2, 3, 4, 5, 6, 7, 8]
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!