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

96
Views
Convierta el objeto de objetos de valor clave en una matriz de objetos del mismo valor clave y devuelva el después de cada iteración

Estoy tratando de convertir un objeto con un objeto dentro en una matriz de objetos. Tengo los datos como:

 "data" :{ "tDetails": { "tName": "Limited", "tPay": " xyz", }, "bDetails": { "bName": "name", "bid": "bid", "bNo": "123456", }, "iDetails": { "iName": "iname", "iBranch": "ibranch", }, }

quiero convertirlo en una matriz de objetos con cada iteración sobre los tres encabezados (tDetails, iDetails,bDetails) como:

 const newData = [ { "id": 1, "title": "tDetails", "content": [ { "id": 1, "key": "tName", "value": "Limited" }, { "id": 2, "key": "tPay", "value": "xyz" } ] }, { "id": 2, "title": "bDetails", "content": [ { "id": 1, "key": "bId", "value": "12345" } ] }, { "id": 3, "title": "iDetails", "content": [ { "id": 1, "key": "iName", "value": "iname" },{ "id":2, "key": "iBranch", "value": "ibranch" } ] }, ]
 const NewData = () => { let newDetails = []; let newHeader = ''; for (const header in data) { // header here is 'tDetails', bDetails, iDetails const headerData = data[header]; newDetails = Object.entries(headerData).map(([key, value]) => ({ key, value, })); newHeader = header; } return [ { title: newHeader, content: newDetails, }, ]; };

Esto me devuelve los datos de solo la última parte (iDetails) tal como se devuelve después del bucle for. Lo que debe cambiarse aquí

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

0

Asigne las entradas del objeto en lugar de usar for..in y, en la devolución de llamada, devuelva el objeto transformado.

 const data={tDetails:{tName:"Limited",tPay:" xyz"},bDetails:{bName:"name",bid:"bid",bNo:"123456"},iDetails:{iName:"iname",iBranch:"ibranch"}}; const newData = Object.entries(data).map(([title, obj], i) => ({ title, id: i + 1, content: Object.entries(obj).map(([key, value], j) => ({ id: j + 1, key, value })) })); console.log(newData);

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!