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

142
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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