按对象数组javascript中的多个键进行分组

sta*_*r67 5 javascript arrays grouping object

我有一个嵌套的对象数组,如下所示。

[
    {
        "region": null,
        "country": null,
        "territory": "Worldwide",
        "territoryCode": "ALL",
        "t2": null,
        "t3": null,
        "t4": null,
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 1,
            "localLanguageName": "N/A",
            "localLanguageCode": null
        }
    },
    {
        "region": "Africa",
        "country": "Madagascar",
        "territory": null,
        "territoryCode": "MG",
        "t2": "AFR",
        "t3": "MG",
        "t4": null,
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 30,
            "localLanguageName": "Malagasy, French",
            "localLanguageCode": "MLG, FRE"
        }
    },
    {
        "region": "Africa",
        "country": null,
        "territory": null,
        "territoryCode": "AFR",
        "t2": "AFR",
        "t3": null,
        "t4": null,
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 2,
            "localLanguageName": "N/A",
            "localLanguageCode": null
        }
    },
    {
        "region": "Africa",
        "country": "Morocco (incl. Western Sahara)",
        "territory": null,
        "territoryCode": "MA",
        "t2": "AFR",
        "t3": "MA",
        "t4": null,
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 35,
            "localLanguageName": "Arabic, French",
            "localLanguageCode": "ARA, FRE"
        }
    },
    {
        "region": "Africa",
        "country": "Morocco (incl. Western Sahara)",
        "territory": "Morocco (excl. Western Sahara)",
        "territoryCode": "MAXEH",
        "t2": "AFR",
        "t3": "MA",
        "t4": "MAXEH",
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 36,
            "localLanguageName": "Arabic, French",
            "localLanguageCode": "ARA, FRE"
        }
    },
    {
        "region": "Africa",
        "country": "Morocco (incl. Western Sahara)",
        "territory": "Western Sahara",
        "territoryCode": "EH",
        "t2": "AFR",
        "t3": "MA",
        "t4": "EH",
        "t5": null,
        "t6": null,
        "t7": null,
        "localLanguage": {
            "territoryId": 37,
            "localLanguageName": "Arabic, French",
            "localLanguageCode": "ARA, FRE"
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

我希望根据独特的地区、国家/地区、t2-t7 组合对整个数据对象进行分组,并获得如下输出

[{
  "region": "Africa",
  "country": [{
      "desc": "Madagascar",
      "t2": [{
        "id": "AFR",
        "localLanguageName": "Malagasy, French",
        "localLanguageCode": "MLG, FRE"
        "t3": [{
          "id": "MG"
        }]
      }]
    },
    {
      "desc": "Morocco (incl. Western Sahara)",
      "subTerritory": [{
        "t2": "AFR",
        "t3": [{
          "id": "MA",
          "localLanguageName": "Arabic, French",
          "localLanguageCode": "ARA, FRE"
          "t4": [{
              "id": "MAXEH",
              "localLanguageName": "Arabic, French",
              "localLanguageCode": "ARA, FRE"
              "t5": [{
                "id": ""
                  .
                  .
                  .
              }]
            },
            {
              "id": "EH",
              "localLanguageName": "Arabic, French",
              "localLanguageCode": "ARA, FRE"
              "t5": [{
                "id": ""
                  .
                  .
                  .
              }]
            }]
        }]
      }]
    }]
}]

Run Code Online (Sandbox Code Playgroud)

我正在寻找对数据进行分组的最有效方法。使用哈希图更好吗?或者 Javascript 中的 map/reduce 方法?

我已经尝试过以下方法。它显然不完整,但经过几次迭代后我陷入了困境。

    const result = Object.values(data.reduce((key, curr) => {
        const { region, country, t2, t3, t4, t5, t6, t7 } = curr;
        if (!key[country]) {
            let obj = {};
            obj.region = region;
            obj.country = country;
            obj.t2 = [{
                id: t2,
                t3: [{
                    id: t3,
                    t4: {
                        id: t4,
                        t5: t5
                    }
                }]
            }];
            key[country] = obj;
        } else {
            key[country].t2 = key[country].t2 || [];
            const foundCountry = key[country].t2.find(x => x.desc === t2);
            if (!foundCountry) {
                key[country].t2.push({
                    id: t2,
                    t3: [{
                        id: t3,
                        t4: {
                            id: t4,
                            t5: t5
                        }
                    }]
                });
            } else {
                const tx = foundCountry.find(x => x.id === t3);
                if (!tx) {
                    foundCountry.push({
                        id: t3,
                        t4: {
                            id: t4,
                            t5: t5
                        }
                    });
                } else {
                    tx.id = t3;
                    tx.t4 = t4;
                }
            }
        }
        return key;
    }, {}));
    console.log(util.inspect(result, false, null, true))
    return result;
Run Code Online (Sandbox Code Playgroud)

Moh*_*ony 0

尝试这个

var data = [
  {
    city: 'LAKE GENEVA',
    state: 'WISCONSIN',
    theatreId: '000080',
    theatreDescription: 'GENEVA 4'
  },
  {
    city: 'BURLINGTON',
    state: 'WISCONSIN',
    theatreId: 'c05364',
    theatreDescription: 'PLAZA THEATRE 4'
  }
]

let group = data.reduce((r, a) => {
 r[a.state] = [...r[a.state] || [], a];
 return r;
}, {});

let nowObj ={};
for (let [key, value] of Object.entries(group)) {
  let groupNew = value.reduce((r, a) => {
      r[a.city] = [...r[a.city] || [], a];
      return r;
  }, {});
  nowObj[key] =groupNew;
}
let mainObj=[];
for (let [key, value] of Object.entries(nowObj)) {
  let obj ={};
  let city =[];
  for (let [key2, value2] of Object.entries(value)) {
    let cityObj ={};
    cityObj['desc'] =key2;
    cityObj['theatres'] =value2.map(item => {
      const container = {};
      container['id'] = item.theatreId;
      container['des'] = item.theatreDescription;
      return container;
    });
    city.push(cityObj);
  }
  obj['state'] =key;
  obj['city'] =city;
  mainObj.push(obj);
}

console.log(mainObj);
Run Code Online (Sandbox Code Playgroud)