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

134
Vistas
Manipulating an API response to have part of the Object as a Key?

I have data that comes back from an API as an array of Objects.

const data = 
  [

      { 
        name: "Charles",
        age: 42, 
        height: 76
      },
      { 
        name: "Jim",
        age: 32, 
        height: 56
      },
      { 
        name: "Ed",
        age: 22, 
        height: 76
      }
  ]

rather than just returning that, return data, I'd like to have it where the name is the key so that I can do a lookup on data, like data["Jim"] and get the object back.

const data = 
      [
         {
          "Charles": 
             { 
               age: 42, 
               height: 76
            },
         },
         {
          "Jim": 
             { 
               age: 32, 
               height: 56
            },
         }
     ]

how can I manipulate the data to come back that way after getting it back from the API in the original format listed first?

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

0

What you're looking for is this

const result = data.reduce((acc, curr) => ({...acc, [curr.name]: {age: curr.age, height: curr.height}}), {});

now you can access like result['Jim']. You needed an object, not an array.

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