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

151
Views
how to convert an object to an array and pull data out of it

I have an object, I'm trying to convert it to an array first and loop through the array and print it in the console but still, it does not destruct the array, it gives me an object.

const person = [

    {
        firstName : 'John',
        age : '24'
    }
];

const btn = document.createElement('button');
btn.innerText = 'Click';
document.body.appendChild(btn);

btn.addEventListener('click', ()=>{

    let newarray = Object.entries(person);

    newarray = person.map((get)=>{

        const {firstName, age} = get;
        return {firstName, age};
    });

    console.log(newarray);

});```
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

If you want nested array in your newArray then do the following:

newarray = person.map((get)=>{

        const {firstName, age} = get;
        return [firstName, age];
    });
about 4 years ago · Santiago Trujillo Report

0

What do you mean by you are trying to convert it to an array? To get the key values you can use Object.keys or for values you can use Object.values.

about 4 years ago · Santiago Trujillo Report

0

Some points to clarify:

Variable const person = [{}] would be better named as persons, as an array of multiple persons, to avoid confusion to others. Also get variable in map would be better as person or per or p.

Immediate assignment without using the variable is useless:

let newarray = Object.entries(person); // useless assignment

newarray = person.map(...);

You need to clearly indicate what is your Input object, and how you expect your Output Array to be like.

I'm thinking that your input is Array of Object, and you want an Array of Array of Values in orders like:

// output
[ ['John', 24], ['Bob', 25], ['Carl', 23] ]

If it's the case, then you can use Object.values to convert object into Array of values:

const persons = [
    {
        firstName : 'John',
        age : 24
    }
];

const btn = document.createElement('button');
btn.innerText = 'Click';
document.body.appendChild(btn);

btn.addEventListener('click', () => {

    let newarray = persons.map((person) => {
        return Object.values(person);
    });

    console.log(newarray); // [ ['John', 24] ]
});
about 4 years ago · Santiago Trujillo 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!