javascript - 如何对新数组进行分组?
问题描述
如何对新数组进行分组?我有需要按此顺序分组的数据,
共有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] }];
}
});
});
}
};
解决方案
推荐阅读
- sicp - SICP 练习 1.18
- python - 计算平均成绩
- ruby-on-rails - 查询所有拥有“.variable?”的用户 Active Storage 图像附件?
- php - 未找到结果时显示消息
- flutter - Flutter:如何在用户移动时获得精确的速度和行进距离?
- flutter - 在 ListView 中滚动单个 ListTile
- c - Apache httpd 自定义模块 libpq PostgreSQL 连接
- powershell - Exchange 服务器上的 Powershell 脚本在 62000 条记录中返回 800 多条记录。为什么?
- c# - 如何在集成测试中从服务中删除 AuthorizeAreaFolder 选项
- docker - 如何使用 docker buildx 顺序构建镜像