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

109
Views
¿Cómo deconstruir una matriz y colocarla en la matriz "principal"?

¿Cómo puedo deconstruir una matriz y colocarla en la matriz en la que se está mapeando y luego continuar clasificándola?

La matriz tiene el siguiente aspecto después de filtrarla:

 [ ['traitName', 'value'], ['traitName', ['value01', 'value02', 'value03' ...] ], //... ]

Quiero verificar cada rasgo y verificar si el primer índice es una matriz, luego deconstruir eso y colocarlo en esta matriz de esta manera:

 [ ['traitName', 'value'], ['traitName', value01], ['traitName', value02], ['traitName', value03], //... ],

Entonces puedo continuar clasificando en esta matriz.

 Object.entries(nft) .filter((val: any) => val[0] !== 'Attributes' && data.attributes[val[0]] !== undefined) .sort((a: any, b: any) => { if (typeof a[1] === 'number') return Infinity; return ( data.attributes[a[0]]?.values[a[1]]?.distribution - data.attributes[b[0]]?.values[b[1]]?.distribution ); })

Necesito esto entre el filtro y la clasificación, creo.

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

0

Puedes usar flatMap para hacer esto:

 const data = [ ['traitName', 'value'], ['traitName', ['value01', 'value02', 'value03']], ] const output = data.flatMap( ([n, v]) => (Array.isArray(v) ? v : [v]).map(vv => [n, vv]) ); console.log(output)

about 4 years ago · Juan Pablo Isaza Report

0

Esto se puede lograr usando .map , .concat y .flat() , en una línea como se muestra a continuación:

 arr.map(([x, y]) => ([].concat(y).map(z => ([x, z])))).flat()

Una posible solución se muestra a continuación:

Fragmento de código

 const arr = [ ['traitName', 'value'], ['traitName', ['value01', 'value02', 'value03'] ] ]; const res = arr.map( ar => ( [].concat(ar[1]) .map(x => ([ar[0], x])) ) ) .flat(); console.log('result using .map & .flat: ', res); // or in just one line: console.log( 'one line result: ', arr.map(([x, y]) => ([].concat(y).map(z => ([x, z])))).flat() );

Explicación

  • Iterar sobre la matriz original
  • Use [].concat() para tratar el segundo elemento de cada matriz interna como una matriz
  • Use .map() para iterar sobre los elementos de la matriz interna
  • Devolver el resultado deseado
  • Use .flat() para aplanar la matriz resultante.
about 4 years ago · Juan Pablo Isaza Report

0

puedes usar esto,

 const a = [ ['traitName', 'value'], ['traitName', ['value01', 'value02', 'value03'] ] ] let final = [] a.forEach((item)=>{ console.log(typeof item[1]) if(typeof item[1] == "string") { final.push(item) } else if(typeof item[1] == 'object') { let len = item[1].length item[1].forEach((item2) =>{ let e = [item[0], item2] final.push(e) }) } }) console.log(final)
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!