我使用TypeScript作为Web应用程序的后端,我发现交集类型对于有效的SQL查询非常有用。基本上,如果我有以下表:如何按子对象对一组对象进行分组?
User
userId: number
userEmail: string
Post
postId: number
userId: number (FK)
postBody: string
我可以与交叉点型(用户&邮政),看起来像这样结束了:
{
userId: number;
userEmail: string;
postId: number;
postBody: string;
}
这意味着我可以使用这种类型来表示我从联合选择查询中返回的行。
问题是,我不得不拆开Web服务器中的数据。我必须编写迭代代码来为每个查询分组,这可以重复。这里是改造的,我试图让类型:
在:
[
{
userId: 1,
userEmail: '[email protected]',
postId: 1,
postBody: 'User 1\'s first post',
},
{
userId: 1,
userEmail: '[email protected]',
postId: 2,
postBody: 'User 1\'s second post',
},
{
userId: 2,
userEmail: '[email protected]',
postId: 3,
postBody: 'User 2\'s first post',
},
]
日期:
[
{
userId: 1,
userEmail: '[email protected]',
posts: [
{
postId: 1,
postBody: 'User 1\'s first post',
},
{
postId: 2,
postBody: 'User 1\'s second post',
}
],
},
{
userId: 2,
userEmail: 'User 2\'s email',
posts: [
{
postId: 3,
postBody: 'User 2\'s first post',
}
]
}
]
我试图想出一个功能,我可以用它来做到这一点动态地,可能传入集合,父键名称的数组以及子集合的名称。我结束了与一个不成功的功能与以下签名:function group(coll: Array<any>, parentKeys: Array<string>, childCollName: string): Array<any>;
我想知道是否有人可以帮助我实现这一点。
到目前为止,我已经尝试过使用Lodash。但是,它的groupBy
函数似乎不能说明子对象是否相等,并且在本例中它仍然给出了三个对象的数组。