我有一个嵌套的对象数组,大致如下所示。
[
{
"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"
}
}
]
我想按独特的地区、国家、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": ""
.
.
.
}]
}]
}]
}]
}]
}]
我正在寻找最有效的方式来对数据进行分组。使用哈希表是否更好?还是在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;
country
有不同的对象结构,一个带有t2
,另一个带有subTerritory
?这是什么原因? - Nina Scholz