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

225
Views
¿Cómo convertir un objeto de solo valores de matriz en una matriz de objetos, cada uno con valores de propiedad transpuestos?

¿Cuál es la forma más eficiente de convertir los datos a continuación en node.js? Cualquier ayuda sería muy apreciada. ¡Gracias!

Tengo algunos datos que se ven así ->

 const inputdata = { data1: [5.0, 10.0, 50.0, 100.0, 500.0], data2: [5.0, 10.0, 50.0, 100.0, 500.0], data3: [5.0, 10.0, 50.0, 100.0, 500.0], };

y necesito convertirlo en esto ->

 [ { data1: 5.0, data2: 5.0, data3: 5.0 }, { data1: 10.0, data2: 10.0, data3: 10.0 }, { data1: 50.0, data2: 50.0, data3: 50.0 }, { data1: 100.0, data2: 100.0, data3: 100.0 }, { data1: 500.0, data2: 500.0, data3: 500.0 }, ]

Intento de código ->

 const inputdata = { data1: [5.0, 10.0, 50.0, 100.0, 500.0], data2: [5.0, 10.0, 50.0, 100.0, 500.0], data3: [5.0, 10.0, 50.0, 100.0, 500.0], }; const outputdata = []; Object.keys(inputdata).forEach(key => { let value = inputdata[key]; if (key === "data1"){ for (let i = 0; i < value.length; i++) { outputdata.push({ data1: 0, data2: 0, data3: 0 }); } } } Object.keys(inputdata).forEach(key => { let value = inputdata[key]; if (key === "data1") { for (let i = 0; i < value.length; i++) { outputdata[i].data1 = value[i]; } } if (key === "data2") { for (let i = 0; i < value.length; i++) { outputdata[i].data2 = value[i]; } } if (key === "data3") { for (let i = 0; i < value.length; i++) { outputdata[i].data3 = value[i]; } } } console.log(inputdata); console.log(outputdata);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Podría transponer los datos y construir nuevos objetos a partir de los datos.

 const data = { data1: [5, 10, 50, 100, 500], data2: [5, 10, 50, 100, 500], data3: [5, 10, 50, 100, 500] }, result = Object .entries(data) .reduce((r, [k, a]) => a.map((v, i) => ({ ...r[i], [k]: v })), []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Técnicas utilizadas del siguiente código de ejemplo paso a paso...

  • Object.assign

  • Object.values

  • Array.prototype.map

  • Array.prototype.reduce

 // reducer function which transposes // a table into a matrix and vice versa. function transpose(result, row) { return row.reduce((matrix, value, idx) => { (matrix[idx] ??= []).push(value); return matrix; }, result); } const inputdata = { data1: [5.0, 10.0, 50.0, 100.0, 500.0], data2: [5.0, 10.0, 50.0, 100.0, 500.0], data3: [5.0, 10.0, 50.0, 100.0, 500.0] }; console.log( Object // convert `inputdata` into a table // ... an array of row like arrays. .values(inputdata) ); console.log( Object // - convert `inputdata` into a table // ... an array of row like arrays. .values(inputdata) // - then transpose the table into a matrix. .reduce(transpose, []) ); console.log( Object // - convert `inputdata` into a table // ... an array of row like arrays. .values(inputdata) // - then transpose the table into a matrix. .reduce(transpose, []) // - then convert/map matrix // into an array of objects. .map(vector => vector.reduce((object, value, idx) => Object.assign(object, { [`data${ idx + 1 }`]: value, }), {} ) ) );
 .as-console-wrapper { min-height: 100%!important; top: 0; }

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!