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

137
Views
Javascript: ¿Cómo convertir una lista de objetos con muchos pares clave-valor en un gran objeto anidado?

por ejemplo, de esto:

 const items = [ { "id": 3, "orgId": 2, "mod": "toyota", "part": "wheel", "price": 333 }, { "id": 4, "orgId": 2, "mod": "toyota", "part": "shell", "price": 350 }, { "id": 9, "orgId": 2, "mod": "honda", "part": "wheel", "price": 222 }, { "id": 10, "orgId": 2, "mod": "honda", "part": "shell", "price": 250 } ]

y convertir a:

 items = { "toyota": {"wheel": 333, "shell": 350 }, "honda": {"wheel": 222, "shell": 250 } }

¿Cuál es la forma más elegante?

Estaba tratando de usar .map() pero no estoy seguro de cómo completar la estructura anidada.


Hacer un seguimiento:

Usando este método:

 const structure = {}; prices.forEach((e) => { structure[e.mod] ??= {}; structure[e.mod][e.part] = e.price; });

o:

 result = items.reduce((r, o) => { r[o.mod] ??= {}; r[o.mod][o.part] = o.price; return r; },{});

Tuve el siguiente error:

 Module parse failed: Unexpected token (56:31) File was processed with these loaders: * ./node_modules/@pmmmwh/react-refresh-webpack-plugin/loader/index.js * ./node_modules/react-scripts/node_modules/babel-loader/lib/index.js You may need an additional loader to handle the result of these loaders. | const structure = {}; | prices.forEach(e => { > structure[e.mod] ??= {}; | structure[e.mod][e.part] = e.price; | });

He buscado mucho pero no encuentro como solucionarlo.

¿Algún consejo?

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

0

Puede usar array#reduce para agrupar según mod .

 const items = [ { "id": 3, "orgId": 2, "mod": "toyota", "part": "wheel", "price": 333 }, { "id": 4, "orgId": 2, "mod": "toyota", "part": "shell", "price": 350 }, { "id": 9, "orgId": 2, "mod": "honda", "part": "wheel", "price": 222 }, { "id": 10, "orgId": 2, "mod": "honda", "part": "shell", "price": 250 } ], result = items.reduce((r, o) => { r[o.mod] ??= {}; r[o.mod][o.part] = o.price; return r; },{}); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Podrías usar el siguiente código:

 const transformedItems = items.reduce((acc, item) => { acc[item.mod] = { ...acc[item.mod], [item.part]: item.price } return acc }, {}) console.log(transformedItems)
about 4 years ago · Juan Pablo Isaza Report

0

También puede iterar a través de los elementos y completar el objeto de result como se muestra a continuación:

 const items = [ { "id": 3, "orgId": 2, "mod": "toyota", "part": "wheel", "price": 333 }, { "id": 4, "orgId": 2, "mod": "toyota", "part": "shell", "price": 350 }, { "id": 9, "orgId": 2, "mod": "honda", "part": "wheel", "price": 222 }, { "id": 10, "orgId": 2, "mod": "honda", "part": "shell", "price": 250 } ]; const result = {}; items.forEach((e) => { result[e.mod] = {...result[e.mod], [e.part]: e.price}; }); console.log(result);

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!