如何按字段值多次对对象数组进行分组?
我有一个从 API 响应中获得的对象数组,并且希望对其进行格式化,以便它可以用于具有嵌套项目的菜单。
[
{
"id": 1,
"name": "The Cool Association",
"description": "the office",
"country": "United Kingdom",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -3.97369,
"y": 55.8665
},
"mainCategory": Hills,
"subCategory": 17,
"refCode": "airdrie"
},
{
"id": 2,
"name": "Test Locations",
"description": "testing",
"country": "United Kingdom",
"county": "Aberdeenshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Mountains,
"subCategory": null,
"refCode": "coat"
},
{
"id": 3,
"name": "Test Locations",
"description": "testing",
"country": "Scotland",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Main Category,
"subCategory": null,
"refCode": "test"
},
{
"id": 4,
"name": "NL Leisure",
"description": "leisure",
"country": "United Kingdom",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Sports,
"subCategory": null,
"refCode": "hello"
}
]
我希望首先按国家/地区对响应进行分组,然后按县对国家内的响应进行分组,然后按主要类别对县内的响应进行分组。这可能吗?
[
"Scotland":{
"North Lanarkshire":{
"MainCategory":{
"...details"
}
}
},
"UnitedKingdom":{
"North Lanarkshire":{
"Hills":{
"...details"
},
"Sports":{
"...details"
}
},
"Aberdeenshire":{
"Mountains":{
"...details"
}
}
}
]
我尝试了各种reduce方式并使用lodash提供的_.groupBy
函数,但未能最终得到我正在寻找的格式。在 API 级别对其进行格式化会更好吗?任何帮助表示赞赏
I have an array of objects obtained from an API response and am looking to format it so that it can be used for a menu with nested items.
[
{
"id": 1,
"name": "The Cool Association",
"description": "the office",
"country": "United Kingdom",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -3.97369,
"y": 55.8665
},
"mainCategory": Hills,
"subCategory": 17,
"refCode": "airdrie"
},
{
"id": 2,
"name": "Test Locations",
"description": "testing",
"country": "United Kingdom",
"county": "Aberdeenshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Mountains,
"subCategory": null,
"refCode": "coat"
},
{
"id": 3,
"name": "Test Locations",
"description": "testing",
"country": "Scotland",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Main Category,
"subCategory": null,
"refCode": "test"
},
{
"id": 4,
"name": "NL Leisure",
"description": "leisure",
"country": "United Kingdom",
"county": "North Lanarkshire",
"town": "Airdrie",
"address": "test addr 1",
"postcode": null,
"latlong": {
"x": -4.02652,
"y": 55.8611
},
"mainCategory": Sports,
"subCategory": null,
"refCode": "hello"
}
]
I am looking to group the response by Country first, then inside country by County, then inside County by Main Category. Is this possible?
[
"Scotland":{
"North Lanarkshire":{
"MainCategory":{
"...details"
}
}
},
"UnitedKingdom":{
"North Lanarkshire":{
"Hills":{
"...details"
},
"Sports":{
"...details"
}
},
"Aberdeenshire":{
"Mountains":{
"...details"
}
}
}
]
I have attempted all manners of reduce and using the _.groupBy
function provided by lodash but haven't been able to end up with the format I am looking for. Would it be better off formatting it at an API level? Any help appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)