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

117
Views
create a new array from the keys of an Array's Object

I have a array of objects, and by using the foreach or map I want to create new array from its keys:

[{
    "name": "Dentist Specialist",
    "category": "Roles",
    "path": "cde"
},
{
    "name": "Root Canal Therapy",
    "category": "Procedures",
    "path": "abc"
},
{
    "name": "Live Course",
    "category": "Course Type",
    "path": "mfg"
}]

From the above array I need a new ARRAY which will look like this:

[{
    "Roles": "Dentist Specialist"
},
{
    "Procedures": "Root Canal Therapy"
},
{
    "Course Type": "Live Course"
}]

Just replace the 2nd key with the first key and remove the rest.

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

0

You can use map here to achieve the desired result.

arr.map(({ category, name }) => ({ [category]: name }));

or

arr.map((o) => ({ [o.category]: o.name }));

const arr = [
  {
    name: "Dentist Specialist",
    category: "Roles",
    path: "cde",
  },
  {
    name: "Root Canal Therapy",
    category: "Procedures",
    path: "abc",
  },
  {
    name: "Live Course",
    category: "Course Type",
    path: "mfg",
  },
];

const result = arr.map((o) => ({ [o.category]: o.name }));
console.log(result);

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!