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

261
Vistas
How do I .map() through data that is layered?

I'm learning about fetch() and am struggling to overcome an issue. I have some JSON data I am fetching, but the key is the "parent" of each item, eg it looks like:

 products
    p1
      description:"Lorem Ipsum"
      name:"some product"
      price:9.99
    p2
      description:"Dolar sit amet"
      name:"another product"
      price:15.5

I'd like to .map() this data into a new array, as such:

  const newData= data.results.map(item => {
    return {
      id: item.id,
      name: item.name,
      description: item.description,
      price: item.price
    };
  });

But I don't understand how I can iterate through each item and save it's ID without a key.

Would anyone know how I could do this?

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

0

You can do this with Object.entries() function:

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. This is the same as iterating with a for...in loop, except that a for...in loop enumerates properties in the prototype chain as well.

Object.entries(data.products).map(([key, value]) => ({
    id: key,
    name: value.name,
    description: value.description,
    price: value.price
}));
about 4 years ago · Juan Pablo Isaza Denunciar

0

Since what you have posted looks like object, rather than array. You have to first transform it into an array. I would suggest using Object.entries()

Object.entries(products).map(([key, value])=> {
 return {
   id: key, 
   name: value.name,
   description: value.description,
   price: value.price
 }
})
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