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

182
Vistas
javascript: create dictionary from array of key-values objects

I have an array of objects.

const ar = [ {0 : "A"}, {1 : "B"}];

I want to create an dictionary from it

const di =  {0 : "A", 1 : "B"};

I am a little struggling with this conversion. How it is possible to do it using map or reduce?

const di = ar.map( ob => (???) }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should use reduce here instead of map. map will give you an array of objects but what you need is an object will all keys. So reduce is the best tool for it.

Main difference between map and reduce

const ar = [{ 0: "A" }, { 1: "B" }];
const di = { 0: "A", 1: "B" };

const result = ar.reduce((acc, curr) => {
  Object.keys(curr).forEach((k) => (acc[k] = curr[k]));
  return acc;
}, {});

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

A simple reduce can be used to merge all objects. Be aware that the keys need to be unique otherwise you will overwrite them.

The merging is done with the spread syntax

const ar = [{0 : "A"}, {1 : "B"}];

const di = ar.reduce((acc, obj) => ({...acc, ...obj}));

console.log(di);

Another simple approach is the use of Object.assign

const ar = [{0 : "A"}, {1 : "B"}];

const di = Object.assign({}, ...ar);

console.log(di);

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