【问题标题】:GroupBy array of objectsGroupBy 对象数组
【发布时间】:2020-07-10 23:21:06
【问题描述】:

我有这个对象数组

[
  {
    "countryCode": "US",
    "countryName": "United States",
    "stateId": "1",
    "stateName": "Alabama",
    "cityId": "1",
    "cityName": "Montgomery"
  },
  {
    "countryCode": "US",
    "countryName": "United States",
    "stateId": "2",
    "stateName": "Alabama",
    "cityId": "2",
    "cityName": "Birmingham"
  },
  {
    "countryCode": "US",
    "countryName": "United States",
    "stateId": "2",
    "stateName": "Alaska",
    "cityId": "1",
    "cityName": "Anchorage"
  }
]

我想转换为如下对象

{
  "countryCode": "US",
  "countryName": "United States",
  "states": [
    {
      "stateId": 1,
      "stateName": "Alabama",
      "cities": [
        {
          "cityId": 1,
          "cityName": "Montgomery"
        },
        {
          "cityId": 2,
          "cityName": "Birmingham"
        }
      ]
    },
    {
      "stateId": 2,
      "stateName": "Alaska",
      "cities": [
        {
          "id": 1,
          "name": "Anchorage"
        }
      ]
    }
  ]
}

我尝试了 lodash 的 groupByvar grouped = _.groupBy(data, 'countryCode'),但我得到的是

{
  HN: [
    {
      countryCode: 'US',
      countryName: 'United States',
      stateId: '1',
      stateName: 'Alabama',
      cityId: '1',
      cityName: 'Montgomery'
    },
    {
      countryCode: 'US',
      countryName: 'United States',
      stateId: '1',
      stateName: 'Alabama',
      cityId: '2',
      cityName: 'Birmingham'
    },
    {
      countryCode: 'US',
      countryName: 'United States',
      stateId: '2',
      stateName: 'Alaska',
      cityId: '1',
      cityName: 'Anchorage'
    }
  ]
}

我不希望将数据分组依据的属性名称的值作为键,我希望将分组依据的属性名称设置为键,然后创建一个自定义属性作为数组列出一个州的所有城市,有没有办法做到这一点?

谢谢!

【问题讨论】:

  • 我建议使用诸如 reduce 之类的运算符,而不是尝试让 groupBy 或类似的运算符以某种方式工作。您将花费更多时间来使用 lo dash 速记而不是使用 reduce。
  • 谢谢@AlexanderStaroselsky,我会记住的。

标签: javascript arrays object lodash


【解决方案1】:

我认为这就是您要寻找的。这也是几乎Javascript group objects by property的副本

x = [{
        "countryCode": "US",
        "countryName": "United States",
        "stateId": "1",
        "stateName": "Alabama",
        "cityId": "1",
        "cityName": "Montgomery"
    },
    {
        "countryCode": "US",
        "countryName": "United States",
        "stateId": "1",
        "stateName": "Alabama",
        "cityId": "2",
        "cityName": "Birmingham"
    },
    {
        "countryCode": "US",
        "countryName": "United States",
        "stateId": "2",
        "stateName": "Alaska",
        "cityId": "1",
        "cityName": "Anchorage"
    }
];

var stateArray = Object.values(x.reduce((result, {
    countryCode,
    countryName,
    stateId,
    stateName,
    cityId,
    cityName
}) => {
    // Create new group
    if (!result[0]) result[0] = {
        countryCode,
        countryName,
        states: []
    };
    // Append to group
    let state = -1;
    for (let i = 0; i < result[0].states.length; i++) {
        if (result[0].states[i].stateId == stateId)
            state = i;
    }
    if (state == -1) {
        result[0].states.push({
            stateId,
            stateName,
            cities: [{
                cityId,
                cityName
            }]
        });
    } else {
        result[0].states[state].cities.push({
            cityId,
            cityName
        });
    }
    return result;
}, {}));

console.log(stateArray)

【讨论】:

  • 是的,正是我想要的,谢谢!但是在寻找类似解决方案时没有发现这个问题,应该多查一下,无论如何谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 1970-01-01
  • 2023-01-03
相关资源
最近更新 更多