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

130
Vistas
How to create a vanilla groupBy so that it returns an array of objects that have a title?

There is a function that groups an array of objects by key. For example an array:

     data: [
        {
          CS_NAME: "AAA",
          IS_MAIN: "Y",
          WEBSITE_CREATE_DATE: "2021-06-01T15:50:37.687",
        },
        {
          CS_NAME: "AAA",
          IS_MAIN: "N",
          WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07",
        },
        {
          CS_NAME: "BBB",
          IS_MAIN: "Y",
          WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07",
        },
        {
          CS_NAME: "BBB",
          IS_MAIN: "N",
          WEBSITE_CREATE_DATE: "2019-01-26T00:00:00",
        },
        {
          CS_NAME: "CCC",
          IS_MAIN: "Y",
          WEBSITE_CREATE_DATE: "2019-01-26T00:00:00",
        },
      ]

Function groupBy:

    groupBy(input, key) {
    
              return input.reduce((acc, currentValue) => {
                let groupKey = currentValue[key];
                if (!acc[groupKey]) {
                  acc[groupKey] = [];
                }
                acc[groupKey].push(currentValue);
                return acc;
              }, {});
            },
        
        let obj = groupBy(data, "CS_NAME");

How to change this function so that it returns an array of objects, each of which will have two fields:

    {
       title: "CS_NAME" // the key by which objects were grouped or any other property
       content: {obj} // the whole object
    }

For example, the output should be like this:

    {
      title: "AAA",
      content: [
        {
          CS_NAME: "AAA",
          IS_MAIN: "Y",
          WEBSITE_CREATE_DATE: "2021-06-01T15:50:37.687",
        },
    
        {
          CS_NAME: "AAA",
          IS_MAIN: "N",
          WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07",
        },
      ],
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You could map the groupd values and add some properties.

const
    groupBy = (input, key) => input.reduce((acc, currentValue) => {
        (acc[currentValue[key]] ??= []).push(currentValue);
        return acc;
    }, {}),
    data = [{ CS_NAME: "AAA", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2021-06-01T15:50:37.687" }, { CS_NAME: "AAA", IS_MAIN: "N", WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07" }, { CS_NAME: "BBB", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07" }, { CS_NAME: "BBB", IS_MAIN: "N", WEBSITE_CREATE_DATE: "2019-01-26T00:00:00" }, { CS_NAME: "CCC", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2019-01-26T00:00:00" }],
    result = Object
        .entries(groupBy(data, "CS_NAME"))
        .map(([title, content]) => ({ title, content }));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can update your groupBy function as below to get the desired output. Get the output of the current output with Object.entries and map to an array again.

const data = [ { CS_NAME: "AAA", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2021-06-01T15:50:37.687", }, { CS_NAME: "AAA", IS_MAIN: "N", WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07", }, { CS_NAME: "BBB", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2021-08-03T12:02:58.07", }, { CS_NAME: "BBB", IS_MAIN: "N", WEBSITE_CREATE_DATE: "2019-01-26T00:00:00", }, { CS_NAME: "CCC", IS_MAIN: "Y", WEBSITE_CREATE_DATE: "2019-01-26T00:00:00", }, ];

const groupBy = (input, key) => {
  return Object.entries(
    input.reduce((acc, currentValue) => {
      let groupKey = currentValue[key];
      if (!acc[groupKey]) {
        acc[groupKey] = [];
      }
      acc[groupKey].push(currentValue);
      return acc;
    }, {})
  ).map(([title, content]) => ({ title, content }));
};

const obj = groupBy(data, "CS_NAME");

console.log(obj);

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