你能帮我写一个我需要的查询吗?我需要 groupBy(smileTypeId) 和 row countBy(id) 其中 postId orderBy(count) desc take(2)。
从 RxJS 商店获取 Smiles 数组
posts = [
{ "smileTypeId": 100000, "postId": 100068, "id": 100403 },
{ "smileTypeId": 100001, "postId": 100046, "id": 100404 },
{ "smileTypeId": 100004, "postId": 100068, "id": 100405 },
{ "smileTypeId": 100000, "postId": 100025, "id": 100406 },
{ "smileTypeId": 100003, "postId": 100006, "id": 100407 },
{ "smileTypeId": 100001, "postId": 100068, "id": 100408 },
{ "smileTypeId": 100000, "postId": 100067, "id": 100409 },
{ "smileTypeId": 100003, "postId": 100068, "id": 100410 },
{ "smileTypeId": 100005, "postId": 100056, "id": 100411 },
.
.
.
]
结果示例:
微笑类型Id | 数 |
---|---|
100005 | 36 |
100002 | 25 |
getSmilesCountByPostId(id: number) {
let pSmilesCount: { smileTypeId: Number, count: Number }[] = [];
if (this.pSmiles.filter(item => item.postId === id).length > 0) {
const pSmiles = this.pSmiles.filter(item => item.postId === id).reduce((count, current) => (count[current.smileTypeId] = count[current.smileTypeId] + 1 || 1, count), []);
Object.entries(pSmiles).forEach(item => pSmilesCount.push({ smileTypeId: Number(item[0]), count: item[1] }));
pSmilesCount.sort((a, b) => (a.count > b.count) ? -1 : ((b.count > a.count) ? 1 : 0));
}
return pSmilesCount;
}
我找到了这样的解决方案,我现在会继续这样做