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

134
Views
Reordenar un objeto para que las claves estén en el mismo orden que una matriz

Tengo una matriz que se ve así,

 ['event_tag', 'workflow_tag', 'created_timestamp', 'success']

y una matriz de objetos donde se ve el objeto,

 { "created_timestamp": "2022-04-01T13:14:53.028002Z", "workflow_tag": "dj807", "event_tag": "refresh", "success": true }

Lo que quiero hacer es hacer que el objeto anterior y cualquier otro objeto en esa matriz coincidan con el orden de los valores en la primera matriz para que el objeto terminado se vea así,

 { "event_tag": "refresh", "workflow_tag": "dj807", "created_timestamp": "2022-04-01T13:14:53.028002Z", "success": true }

He intentado lo siguiente hasta ahora,

 const keys = ['event_tag', 'workflow_tag', 'created_timestamp', 'success']; newRowData = parsedRows.reduce((obj, v) => { obj[v] = keys[v]; return obj }, {});

Pero esto vuelve,

 {[object Object]: undefined}
about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

En lugar de iterar sobre las filas, iterar sobre las claves, ya sea mapear/reducir.

 const keys = ["event_tag", "workflow_tag", "created_timestamp", "success"]; const obj = { created_timestamp: "2022-04-01T13:14:53.028002Z", workflow_tag: "dj807", event_tag: "refresh", success: true, }; const res = Object.assign({}, ...keys.map((key) => ({ [key]: obj[key] }))); console.log(res)

about 4 years ago · Santiago Gelvez Report

0

Puede ordenar las claves construyendo un nuevo objeto dentro de un Array#map :

 const parsedRows = [ { a: 1, c: 3, d: 4, b: 2, }, { b: 6, a: 5, c: 7, d: 8, }, { d: 12, b: 10, a: 9, c: 11, }, ]; const order = ['a', 'b', 'c', 'd']; let newData = parsedRows.map(row => { let newRow = {}; for (let key of order) { newRow[key] = row[key]; } return newRow; }); console.log(newData);

about 4 years ago · Santiago Gelvez 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!