Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

51
Vistas
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.

7 months ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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);

7 months ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos