gpt4 book ai didi

javascript - 在 lodash 中使用双 groupBy

转载 作者:行者123 更新时间:2023-11-30 14:15:41 28 4
gpt4 key购买 nike

所以我试图按某个属性对一组对象进行分类。第一次使用 groupBy 效果很好。现在我需要遍历这些分组并根据单独的属性再次对它们进行分组。我遇到了这方面的问题,有人可以帮助我吗?

TS

this.accountService.getAccountListWithBalance().subscribe(accounts => {
this.accountList = _.groupBy(accounts, 'category');
for (var property in this.accountList) {
if (this.accountList.hasOwnProperty(property)) {
this.accountList.property = _.groupBy(this.accountList.property, 'subcategory');
}
}

generateArray(obj){
return Object.keys(obj).map((key)=>{ return {key:key, value:obj[key]}});
}

HTML:

<ul *ngFor="let item of generateArray(accountList)">
<strong>{{ item.key }}</strong>
<li *ngFor="let i of item.value">{{i.name}}</li>
</ul>

HTML 没有为下一级交互设置,但我知道如果我只是控制台记录生成的对象,它是行不通的。就像我说的,它是第一次排序,而不是第二次。

最佳答案

我只需更改语法就可以让它工作。使用 [] 而不是 . 因此工作代码如下。

this.accountService.getAccountListWithBalance().subscribe(accounts => {
this.accountList = _.groupBy(accounts, 'category');
for (var property in this.accountList) {
if (this.accountList.hasOwnProperty(property)) {
this.accountList[property] = _.groupBy(this.accountList[property], 'subcategory');
}
}

关于javascript - 在 lodash 中使用双 groupBy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53601620/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com