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

143
Views
How to move a key value to an new array inside an object

From below object

{
    "user": {
        "id": 9,
        "email": "abcd@example.com",
    },
    "country": "USA",
    "first_name": "firstName",
    "last_name": "lastName",
}

How can we move first_name and last_name to a new array named names and push that into the same object as below

{
    "user": {
        "id": 9,
        "email": "abcd@example.com",
    },
    "country": "USA",
    "name": [
        {
            "first_name": "firstName",
            "last_name": "lastName",
        }
    ],
}

Thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Destructure the first_name and last_name from the rest of the properties, then create a new object; spreading out the old saved properties, and adding an array with an object with first_name and last_name properties.

const obj={user:{id:9,email:"abcd@example.com"},country:"USA",first_name:"firstName",last_name:"lastName"};

const { first_name, last_name, ...rest } = obj;

const newObj = {
  ...rest,
  names: [ { first_name, last_name } ]
};

console.log(newObj);

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!