【发布时间】:2017-02-07 03:49:14
【问题描述】:
使用 lodash,我如何将具有相同生日的人的姓名分组,如下所示?
我使用嵌套的 for 循环将其写出来,但我认为在 lodash 中会有更优雅的方式来执行此操作。我没有使用太多,并试图弄清楚要使用哪个功能。
[
{ "birthdate": "1993", "name": "Ben" },
{ "birthdate": "1994", "name": "John" },
{ "birthdate": "1995", "name": "Larry" },
{ "birthdate": "1995", "name": "Nicole" },
{ "birthdate": "1996", "name": "Jane" },
{ "birthdate": "1996", "name": "Janet" },
{ "birthdate": "1996", "name": "Dora" },
]
到
[
{ "birthdate": "1993", "names": [ "Ben" ] },
{ "birthdate": "1994", "names": [ "John"] },
{ "birthdate": "1995", "names": [ "Larry", "Nicole" ] },
{ "birthdate": "1996", "names": [ "Jane", "Janet", "Dora" ] }
]
【问题讨论】:
标签: lodash