0

How do I group a new array? I have data that needs to be grouped in this order,

There are 3 companies in total:

  1. Apple
  2. Samsung
  3. Xiaomi

I receive data about employees who work in these companies, and they are displayed as in pictures (the second list), but it is necessary that, as in the first case, I try to do it like this: enter image description here

    let uniqueGroups;

    export const Group = [
      {
        title: "Apple",
        index: 1,
      },
      {
        title: "Samsung",
        index: 2,
      },
      {
        title: "Xiaomi",
        index: 3,
      },
    ];

  const data = [
    {_person: 'Alex', _type: 'Apple'},
    {_person: 'Marty', _type: 'Xiaomi'},
    {_person: 'Gloria', _type: 'Apple'},
    {_person: 'Melman', _type: 'Samsung'},
    {_person: 'Bob', _type: 'Xiaomi'},
  ]

 
// reserveList - My data (_type.name - Company name, _person, the person who works for this company)
    const handleRenderGroupItems = () => {
        if (data) {
          return Group.map(({ index, title }) => {
            return data.filter(({ id, _type, _person }) => {
              if (_type=== title) {
                uniqueGroups = [{ id, type: _type, persons: [_person] }];
              }
            });
          });
        }
      };
reactchel
  • 146
  • 1
  • 2
  • 10

0 Answers0