首页 > 解决方案 > 如何对新数组进行分组?

问题描述

如何对新数组进行分组?我有需要按此顺序分组的数据,

共有3家公司:

  1. 苹果
  2. 三星
  3. 小米

我收到有关在这些公司工作的员工的数据,它们显示为图片(第二个列表),但有必要像第一种情况一样,尝试这样做: 在此处输入图像描述

    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] }];
              }
            });
          });
        }
      };

标签: javascriptreactjs

解决方案


推荐阅读