0

I created a function which creates an arrays of arrays where the inside arrays are created according to user action. First let me show you the function

const groupByArray = () => {
      if (groupBy === PMDashboardGroupBy.NONE || !groupBy) {
        return createDashboardTableData();
      }

      let result: any[] = [];

      createDashboardTableData().forEach(data => {
        if (result.length === 0) {
          result = [[data]];
        } else {
          for (let i = 0; i < result.length; i++) {
            if (
              result[i].some((item: any) => item[groupBy] === data[groupBy])
            ) {
              result[i] = [...result[i], data];
              break;
            } else if (result.length - 1 === i) {
              result.push([data]);
              break;
            }
          }
        }
      });
      return result;
    };

groupBy comes from user input. If the values is 'none' we return the original array e.g.

[
    {
      _id: '1',
      projectTitle: 'Project title 1',
      queryTitle: 'Query title 1',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'WAITING',
      city: 'Prague',
      type: 'My Craft',
    },
    {
      _id: '2',
      projectTitle: 'Project title 1',
      queryTitle: 'Query title 2',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'WAITING',
      city: 'Jihlava',
      type: 'Open Craft',
    },
    {
      _id: '3',
      projectTitle: 'Project title 3',
      queryTitle: 'Query title 1',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'ESTIMATED',
      city: 'Prague',
      type: 'My Craft',
    }
  ]

if it's not 'none' we will return an array of array where the inside group of arrays are grouped by the key/value pair of the object. For instance if user wants to group by projectTitle than the resulting array of arrays would be

[
  [
    {
      _id: '1',
      projectTitle: 'Project title 1',
      queryTitle: 'Query title 1',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'WAITING',
      city: 'Prague',
      type: 'My Craft',
    },
    {
      _id: '2',
      projectTitle: 'Project title 1',
      queryTitle: 'Query title 2',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'WAITING',
      city: 'Jihlava',
      type: 'Open Craft',
    },
  ],
  [
    {
      _id: '3',
      projectTitle: 'Project title 3',
      queryTitle: 'Query title 1',
      submissionDate: '2012-11-04T14:51:06.157Z',
      state: 'ESTIMATED',
      city: 'Prague',
      type: 'My Craft',
    }
  ]
 ]

Is there a way to remove the nested loops (for cycle, some) and make the function more efficient. Thank you for suggestions.

Hayk Shakhbazyan
  • 242
  • 2
  • 15
  • are you having an issue with performance? If you're not, you're optimizing early, and unless this is a learning exercise you better spend your time and efforts elsewhere – Obed Parlapiano Mar 31 '21 at 09:11
  • 1
    Pick the second-voted answer from the duplicate thread. – georg Mar 31 '21 at 09:20

0 Answers0