Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda