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

179
Views
JS Javascript: ¿cómo colocar valores de matriz en otra matriz por índices?

Tengo esta matriz/objeto:

 const template = {0: [0, 1, 2, 3, 4, 5], 1: [0, 1, 2, 3, 4, 6]} // I have the above but happy to convert the below if it helps: //const templateArr = [[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 6]]

Luego tengo una matriz que quiero mapear en esa secuencia:

 const myArray = ["Q","W","A","S","Z","X,"E","R","D"] // for example

Mi intención es tener el siguiente resultado:

 let myResult = [["Q", "W", "A", "S", "Z", "X"],["Q", "W", "A", "S", "Z", "E"]]

Entonces, todos los valores de myArray están en las posiciones establecidas por template .

No estoy seguro si debo usar .map() o algo más... ¿Puede alguien indicarme la dirección correcta?

¡Muchas gracias!

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

0

Sí, .map() es la herramienta adecuada aquí. Puede usar dos, uno externo para mapear sus matrices internas desde templateArr , y luego un mapa interno para mapear los números (índices) en sus matrices internas, lo que transformará (es decir, mapeará) cada número al valor correspondiente en el índice de myArray :

 const templateArr = [[0, 1, 2, 3, 4, 5],[0, 1, 2, 3, 4, 6]]; const myArray = ["Q","W","A","S","Z","X","E","R","D"]; const res = templateArr.map(inner => inner.map(idx => myArray[idx])); console.log(JSON.stringify(res)); // JSON.stringify to pretty-print

about 4 years ago · Juan Pablo Isaza Report

0

Object.values convertirá su template en una matriz de matrices, luego simplemente haga un mapa sobre ella y otro mapa interno sobre los índices y los reemplace con una letra particular de myArray .

Código:

 const template = {0: [0, 1, 2, 3, 4, 5], 1: [0, 1, 2, 3, 4, 6]} const myArray = ["Q","W","A","S","Z","X","E","R","D"] const res = Object.values(template) .map(a => a.map(idx => myArray[idx] ?? null)) console.log(res)
 .as-console-wrapper { max-height: 100% !important; top: 0; } /* ignore this */

about 4 years ago · Juan Pablo Isaza Report

0

Creo que esto debería funcionar:

 const res = templateArr.map( (arr) => { return arr.map( (index) => { return myArray[index] }) })
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!