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

269
Vistas
JavaScript - adding extra keys values to an object using spread operation or alternative approaches

Here is the code:

    let bucket = [[],[],[],[],[],[],[],[],[],[]];

    bucket = {...Object.keys(bucket)
                       .sort((a,b) => b-a)
                       .filter(key => key > 0)
                       .map(key => '-'+key), 
              ...bucket};

   console.log(bucket);

Problem: the first line of code is not adding the negative keys of the original bucket object into the object, with all properties (keys) having empty arrays as their corresponding value.

Bucket only shows its original properties and values after this line of code is evaluated

How can I get this to work?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The Array.map() creates an array of strings with your negative keys, and they are overwritten when you spread the bucket array. Instead create pairs of [new key, []] convert to an object with Object.fromEntries(), and then spread them:

const bucket = [[],[],[],[],[],[],[],[],[],[]];

const result = {
  ...Object.fromEntries(
    Object.keys(bucket)
      .filter(key => key > 0)
      .sort((a, b) => b-a)
      .map(key => [-key, []])
  ),
  ...bucket};

console.log(result);

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