json 按键分组对象并返回新对象[重复]

ycl3bljg  于 2023-03-31  发布在  其他
关注(0)|答案(1)|浏览(134)

此问题在此处已有答案

(19个答案)
How to group an array of objects based on multiple keys in Javascript?(3个答案)
2天前关闭。
我有一个对象数组,我需要在JavaScript中以适合我的方式对其进行排序。
这是一个数组的例子:

[
  {
    "id": "AD012014001R",
    "country": "AD",
    "series": 1,
    "issue_year": 2014
  },
  {
    "id": "AD012014002R",
    "country": "AD",
    "series": 1,
    "issue_year": 2014
  },
  {
    "id": "AT012002002R",
    "country": "AT",
    "series": 1,
    "issue_year": 2002
  },
  {
    "id": "BE011999100R",
    "country": "BE",
    "series": 1,
    "issue_year": 1999
  },
  {
    "id": "BE011999200R",
    "country": "BE",
    "series": 1,
    "issue_year": 1999
  },
  {
    "id": "BE022008001R",
    "country": "BE",
    "series": 2,
    "issue_year": 2008
  }
]

我的输出应该是这样的:

[
  {
    "country": "AD",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "AD012014001R",
            "country": "AD",
            "series": 1,
            "issue_year": 2014
          },
          {
            "id": "AD012014002R",
            "country": "AD",
            "series": 1,
            "issue_year": 2014
          }
        ]
      }
    ]
  },
  {
    "country": "AT",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "AT012002002R",
            "country": "AT",
            "series": 1,
            "issue_year": 2002
          }
        ]
      }
    ]
  },
  {
    "country": "BE",
    "series": [
      {
        "series": 1,
        "coins": [
          {
            "id": "BE011999100R",
            "country": "BE",
            "series": 1,
            "issue_year": 1999
          },
          {
            "id": "BE011999200R",
            "country": "BE",
            "series": 1,
            "issue_year": 1999
          }
        ]
      },
      {
        "series": 2,
        "coins": [
          {
            "id": "BE022008001R",
            "country": "BE",
            "series": 2,
            "issue_year": 2008
          }
        ]
      }
    ]
  }
]

所以我的API响应必须更大,但这只是其中的一小部分。我得到了数百个看起来像这样的对象的数组,需要对它们进行排序,并首先按国家排序,然后按每个国家下的系列返回它们。我知道有很多类似的问题,但我无法弄清楚,因为我的问题比所有问题都复杂。

zf9nrax1

zf9nrax11#

你可以使用一个包含组键和children属性的数组来实现多步操作。然后在级别上找到组并构建一个新的对象,或者只是返回子对象。最后推送对象。

const
    data = [{ id: "AD012014001R", country: "AD", series: 1, issue_year: 2014 }, { id: "AD012014002R", country: "AD", series: 1, issue_year: 2014 }, { id: "AT012002002R", country: "AT", series: 1, issue_year: 2002 }, { id: "BE011999100R", country: "BE", series: 1, issue_year: 1999 }, { id: "BE011999200R", country: "BE", series: 1, issue_year: 1999 }, { id: "BE022008001R", country: "BE", series: 2, issue_year: 2008 }],
    groups = [
        ['country', 'series'],
        ['series', 'coins']
    ],
    result = data.reduce((r, o) => {
        groups
            .reduce((level, [key, children]) => {
                let group = level.find(q => q[key] === o[key]);
                if (!group) level.push(group = { [key]: o[key], [children]: [] });
                return group[children];
            }, r)
            .push(o);
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

相关问题