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

92
Vistas
Transforming a complex object into an array

I am creating a React app using data from an API. the response data is a long complicated object that looks something like this:

{
    "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            "name": "Peas (800g)",
            "category_id": "6181841060c425f76e57b603",
            "slug": "vegetables",
            "quantity": 16,
            "price": {
                "amount": 3.00
            },
            "thumbnail":"URI ...",,
            "images": [
                "URI ...",
            ],
            "synonyms": [
                "Veggies"
            ],
        },

//etc for many key/values inside this object ..
        
}

I need to access the information do display a category ("Frozen Products" in this case) and all the product names and images under it.

But I think I need to access each Key/Value pair by loopin through their index (example data[0]) not the Key name since they keys are dynamically created by an API. So how can I change this to:

{
   [ "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            "name": "Peas (800g)",
            "category_id": "6181841060c425f76e57b603",
            "slug": "vegetables",
            "quantity": 16,
            "price": {
                "amount": 3.00
            },
            "thumbnail":"URI ...",,
            "images": [
                "URI ...",
            ],
            "synonyms": [
                "Veggies"
            ],
        },
   ]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you can loop through an object with for in iterator. No matter what key names are

for(key in object) {
  let value = object[key];
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think you need something like this:

let data = {
    "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            ...
        }
    ],
    "Another Category": [...]    
}

let categories = Object.keys(data).map((key) => ({categoryName: key, products: data[key]}));
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