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

135
Visualizações
How does one create an object from an array of data items?

I'm struggling to create an object from an array in JS. I keep getting an error when pushing into the plots object.

makeArrayFilteredPlots = () => {
  let plots = {};
  this.props.filteredPlots.forEach((plot) => {
    const status = plot.entity.status.slug;
    plots[status].push(plot);
  });
  console.log(plots);
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. In JS, an array has no named keys, it's only a list of things. If you want named keys, use an object {}
  2. plots[status] is never initialized. When you try to .push() stuff in something undefined, the script crashes. Initialize it to an empty array before starting to push things in it.

makeArrayFilteredPlots = () => {
  let plots = {};
  this.props.filteredPlots.forEach((plot) => {
    const status = plot.entity.status.slug;
    plots[status] = plots[status] || []; // Initialize an empty array
    plots[status].push(plot);
  });
  console.log(plots);
};

about 4 years ago · Juan Pablo Isaza Relatório

0

From the above comment ...

"The target format is not even valid JS syntax. One can not clearly see whether the OP wants to generate array items, where each item is an object with a single key (or something else). From the generating code it looks like the OP wants to create/aggregate an object where each entry (key value pair) is an array. But then we are not talking about a multi dimensional array."

Sophisticated guess ... a reduce based task should solve the OP's problem of generating a configuration like object of plot specific arrays ...

const plotsConfig = this
  .props
  .filteredPlots
  .reduce((result, plot) => {

    const plotKey = plot.entity.status.slug;
    const plotList = result[plotKey] ??= [];

    plotList.push(plot);
    
    return 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