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

165
Views
¿Cómo transformar una matriz de objetos para que coincida con objetos en otras matrices?

Hay un arreglo con los siguientes datos

Datos iniciales

 const data = [ { type: 'first' stats: [ {name: 'yes', value: 11}, {name: 'no', value: 33}, ] }, { type: 'second', stats: [ {name: 'call back', value: 32}, {name: 'no', value: 77}, ] }, { type: 'third', stats: [ {name: 'yes', value: 14}, ] } ]

Necesito ayuda para convertir los datos de arriba al siguiente formulario

Resultado Esperado

 const dataTransformed = [ { type: 'first', stats: [ {name: 'yes', value: 11}, {name: 'no', value: 33}, {name: 'call back', value: 0},//*zero because this object exist in third object but here doesnt ] }, { type: 'second', stats: [ {name: 'yes', value: 0},//*zero because this object exist in first object but here doesnt {namename: 'no', value: 77}, {name: 'call back', value: 32}, ] }, { type: 'third', stats: [ {name: 'yes', value: 14}, {name: 'no', value: 0},//*zero because this object exist in first object but here doesnt {name: 'call back', value: 0},//*zero because this object exist in first object but here doesnt ] } ]

Es decir, es necesario que en cada objeto de la matriz de estadísticas el orden de los objetos con el nombre especificado corresponda a otros objetos, mientras que si un objeto con el mismo nombre no tiene un índice correspondiente, debe crearlo con un valor vacío.

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

0

 const data=[{type:"first",stats:[{name:"yes",value:11},{name:"no",value:33}]},{type:"second",stats:[{name:"call back",value:32},{name:"no",value:77}]},{type:"third",stats:[{name:"yes",value:14}]}]; const names = [... new Set(data.reduce((a, x) => a.concat(x.stats.map(y => y.name)), []))]; const dataTransformed = data.map(x => ({ ...x, stats: names.map(name => ({ name, value: (x.stats.find(z => z.name === name) || {value: 0}).value, })), })); console.log(dataTransformed);

Puede crear una matriz de nombres, luego verificar qué nombres se presentan en las estadísticas, usando un valor predeterminado cuando no lo es.

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!