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

发布于 2025-01-10 20:04:23 字数 2481 浏览 0 评论 0原文

我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-01-17 20:04:23
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));

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));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文