1

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",
        },
      ],
    }
Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
AndriiBal
  • 35
  • 1
  • 8

2 Answers2

1

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);
Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
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; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392