Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Best way to convert array to object

What is the best way to convert this

const items = [
  { name: "Leon", url: "../poeple" },
  { name: "Bmw", url: "../car" }
];

into this using javascript :

const result = {
  Leon: "../poeple",
  Bmw: "../car"
};
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could map the objects as entries and get an object from it.

const
    items = [{ name: "Leon", url: "../poeple" }, { name: "Bmw", url: "../car" }],
    result = Object.fromEntries(items.map(({ name, url }) => [name, url]));

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could just reduce the array:

const items = [{
    name: "Leon",
    url: "../people"
  },
  {
    name: "Bmw",
    url: "../car"
  }
];

const o = items.reduce((o, el) => {
  o[el.name] = el.url;
  return o;
}, {});

console.log(o)

The Array.prototype.reduce method is very flexible, and might be used to reduce an array to another entity:

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

about 4 years ago · Juan Pablo Isaza Denunciar

0

There are lots of different approaches as your can see. But if you're new to JavaScript maybe a simple loop over the array to create new keys and values in a new object would be easier to understand.

const items=[{name:"Leon",url:"../poeple"},{name:"Bmw",url:"../car"}];

const out = {};

for (const obj of items) {
  out[obj.name] = obj.url;
}

console.log(out);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda