Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!