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

158
Vistas
how to simplify pop, push and unshift?

I have an array of objects and I would like to extract the object keys and put them in an array of possible object keys. This is the array of objects I started with :

const data = [
    {
        name: "Joe",
        age: 23,
        job: "Artist",
        hoby: "Drawing",
    },
    {
        name: "Michael",
        age: 21,
        job: "Engineer",
        hoby: "Fishing",
    },
    {
        name: "Jenifer",
        age: 22,
        job: "Dentist",
        hoby: "Gardening",
    },
]

I would like to get the following output :

{ header: [ 'number', 'name', 'age', 'job', 'hoby' ],
   data:
   [ { name: 'Joe', age: 23, job: 'Artist', hoby: 'Drawing' },
      { name: 'Michael', age: 21, job: 'Engineer', hoby: 'Fishing' },
      { name: 'Jenifer', age: 22, job: 'Dentist', hoby: 'Gardening' } 
   ] }

Right now I'm using the following code to accomplish this :

data.unshift(header);
data.pop(data)
data.pop(data)
data.pop(data)
data.push(data1);

But it isn't returning what I would like it to return. Does anyone know an answer to my question. Thanks in advance

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

0

You can do something like this:

result = {
  header: [],
  data: data
};

for(const record of data) {
  for(const [key, value] of Object.entries(record)) {
     if(!result.header.includes(key)) {
       result.header.push(key)
     }
  }
}

console.log(result)

But i dont get where your header number comes from...?

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