简体   繁体   English

如何多次按字段值对对象数组进行分组?

[英]How can I group an array of objects by field values multiple times?

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.我有一个从 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"
    }
]

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.我尝试了所有减少和使用_.groupBy提供的 _.groupBy function 的方式,但未能以我正在寻找的格式结束。 Would it be better off formatting it at an API level?将其格式化为 API 级别会更好吗? Any help appreciated任何帮助表示赞赏

 const arr = [{ "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" } ] const result = arr.reduce((acc, curr) => { const country = acc[curr.country] || {}; const county = country[curr.county] || {}; const mainCategory = county[curr.mainCategory] || {}; mainCategory[curr.name] = curr; county[curr.mainCategory] = mainCategory; country[curr.county] = county; acc[curr.country] = country; return acc; }, {}); console.log(JSON.stringify(result, null, 2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM