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

239
Vistas
What does this Object.assign({}, ...array.map(/*...*/)) code do here

I have cloned this repo from github and I am unable to understand what's happening in the code.

words in the code is an array of words.

const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's taking an array (of strings, since you said it's an array of words; but it would work with numbers or Symbols too) and converting it to an object where both the names and values of the properties are the same. So for example, ["an", "example"] becomes {an: "an", example: "example"}:

const words = ["an", "example"];
const wordDictionary = Object.assign({}, ...words.map((x) => ({ [x]: x })));
console.log(wordDictionary);

It does it by mapping each array element to an object with one property (named by the array element, with the value of the array element) and then assigning all of those objects to a single target object.

For instance, with my ["an", "example"] array, the steps are:

  1. Do the mapping, turning:
    ["an", "example"]
    
    into
    [{an: "an"}, {example: "example"}]
    
  2. Spread that array out into discrete arguments to Object.assign with a blank target object. So
    Object.assign({}, ...[{an: "an"}, {example: "example"}])
    
    becomes
    Object.assign({}, {an: "an"}, {example: "example"})
    
  3. Use Object.assign to assign the properties from those objects to the first one (the blank {} object).

It's worth noting that the code is overcomplicated. It would be much simpler just to use a loop:

const wordDictionary = {};
for (const word of words) {
    wordDictionary[word] = word;
}

Separately, dictionaries like this are better handled by Map instances:

const wordDictionary = new Map();
for (const word of words) {
    wordDictionary.set(word, word);
}

or in this specific case (since the key and value are the same) a Set:

const wordDictionary = new Set(words);
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