Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
Javascript: How to convert a list of objects with many key-value pairs into one big nested object?

for example, from this:

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
        }
    ]


and convert to:


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

What is the most elegant way?

I was trying to use .map() but I'm not sure how to complete the nested struture.


Follow up:

Using this method:

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

or:

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

I got the following 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;
|       });

I have done quite a lot searching but cannot find how to solve it.

Any advices?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use array#reduce to group based on 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 Denunciar

0

You could use the code below:

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 Denunciar

0

You could also iterate through items and populate the result object as below:

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda