Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!