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

112
Vistas
How do I transform the data from this API into an array of objects organized by a specific prop?

I'm getting this data from an API:

 let dataFromApi = [
    {
      // lots of other props
      name: 'super cool place',
      address: 'address of food place',
      classification: 'food',
    },
    {
      // lots of other props
      name: 'name of tech place',
      address: 'address of tech place',
      classification: 'tech',
    },
    {
      // lots of other props
      name: 'name of clothes place',
      address: 'address of clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another clothes place',
      address: 'address of another clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another tech place',
      address: 'address of another tech place',
      classification: 'tech',
    },
  ]

What I'd like to do is create a new array of objects that has 2 things:

  1. A prop called classification;

  2. A prop called establishments, which is an array of objects containing all the establishments matching the classification prop.


Meaning something like this:

let establishmentsArray = [
    {
      classification: 'food',
      establishments: [
        {
          name: 'name of food place',
          address: 'address of food place',
          classification: 'food',
          // rest of its props
        },
        // Lots of other food places places
      ],
    },
    {
      classification: 'clothes',
      establishments: [
        {
          name: 'name of clothes place',
          address: 'address of clothes place',
          classification: 'clothes',
          // rest of its props
        },
        {
          name: 'name of another clothes place',
          address: 'address of another clothes place',
          classification: 'clothes',
          // rest of its props
        },
        // Lots of other clothes places
      ],
    },
    {
      classification: 'tech',
      establishments: [
        {
          name: 'name of tech place',
          address: 'address of tech place',
          classification: 'tech',
          // rest of its props
        },
        {
          name: 'name of another tech place',
          address: 'address of another tech place',
          classification: 'tech',
          // rest of its props
        },
        // Lots of other tech places
      ],
    },
  ]

Thanks for the help in advance!

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

0

Looks like you're trying to group by single field which is a good use-case for array.reduce:

let dataFromApi = [
    {
      // lots of other props
      name: 'super cool place',
      address: 'address of food place',
      classification: 'food',
    },
    {
      // lots of other props
      name: 'name of tech place',
      address: 'address of tech place',
      classification: 'tech',
    },
    {
      // lots of other props
      name: 'name of clothes place',
      address: 'address of clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another clothes place',
      address: 'address of another clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another tech place',
      address: 'address of another tech place',
      classification: 'tech',
    },
  ]
  
  let output = dataFromApi.reduce((acc, cur) => {
      let { classification, ...rest} = cur;
      let prev = acc.find(x => x.classification === classification);
      if(!prev) {
          acc.push({classification, establishments: [cur]});
      } else {
          prev.establishments.push(cur);
      }
      return acc;
  }, []);
  
  console.log(output);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is another example, you can use map.reduce:


const groupByClassification = (items) => {
  const grouped = items.reduce((acc, cur) => {
    if (cur.classification in acc) {
      acc[cur.classification].establishments.push(cur);

      return acc;
    }

    const newGroupItem: GroupedItem = {
      classification: cur.classification,
      establishments: [cur]
    };

    return {
      ...acc,
      [cur.classification]: newGroupItem
    };
  }, {} });

  return Object.values(grouped);
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

let dataFromApi = [
    {
      // lots of other props
      name: 'super cool place',
      address: 'address of food place',
      classification: 'food',
    },
    {
      // lots of other props
      name: 'name of tech place',
      address: 'address of tech place',
      classification: 'tech',
    },
    {
      // lots of other props
      name: 'name of clothes place',
      address: 'address of clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another clothes place',
      address: 'address of another clothes place',
      classification: 'clothes',
    },
    {
      // lots of other props
      name: 'name of of another tech place',
      address: 'address of another tech place',
      classification: 'tech',
    },
  ]


function GroupByClassification(data){
  var groupedData = {};
  for (const element of data) {
  if(!(element.classification in groupedData)){
    groupedData[element.classification] = [element];
    }else{
    groupedData[element.classification].push(element);
    }
  }
  return groupedData;
}

console.log(GroupByClassification(dataFromApi));

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