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

140
Visualizações
Calling Object.entries on an empty Array returns something

Using reduce function, I would like to get directors and their titles in a object. I accidentally initialized it as an empty array instead of an object. But I figured out that, calling Object.entries on this empty array was returning something.

And I would like to understand why this returns something. Is this about references ?

Code :

const films  = [
  { director: "Hayao Miyazaki", title: "title1" },
  { director: "Hayao Miyazaki", title: "title2" },
  { director: "Gerard", title: "title1" },
  { director: "Gerard", title: "title1" },
  { director: "Anne", title: "title1" },
];

// we want an object of directors and their titles, using reduce
const directorsFilms = films.reduce((a, { director, title }) => {
  if (!a[director]) a[director] = [];
  a[director].push(title);
  return a;
}, []); // my mistake, should have been an empty object "{}"

// prints something in PythonTutor but not in Codepen.
// referenced to an empty array
console.log(directorsFilms);

// but when you call Object.entries on this empty array, there are results
// I call Object.fromEntries to get the object I was supposed to have calling reduce, not important
const tmp = Object.fromEntries(Object.entries(directorsFilms));
console.log(tmp);

I don't understand how this is possible... I would appreciate some insight. Thanks in advance! :)

about 4 years ago · Juan Pablo Isaza
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