我想根据原始 json 数据和预期的 json 数据重构 json。
如果您仔细查看原始 json 数据,我有男性/女性属性之外的国家/地区。根据国家模块内的方向属性,我希望国家模块位于男性/女性属性内。所以在后数据中,我将在男性属性中有 1 个国家模块(因为有 1 条男性记录),在女性属性中有 2 个国家模块(因为有 2 条女性记录)。
原始 json 数据如下所示:
{
"Implementations": [
{
"Male": {
"Gender": "Male"
},
"Female": {
"Gender": "Female"
},
"Country": [
{
"Orientation": "Male",
"Name": ABCD
},
{
"Orientation": "Female",
"Name": EFGH
},
{
"Orientation": "Female",
"Name": IJKL
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
}
]
}
预期的 json 数据:
{
"Implementations": [
{
"Male": {
"Gender": "Male"
"Country": [
{
"Orientation": "Male",
"Name": ABCD
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
},
"Female": {
"Gender": "Female"
"Country": [
{
"Orientation": "Female",
"Name": EFGH
},
{
"Orientation": "Female",
"Name": IJKL
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
}
}
]
}
程序:
var Implementations = {
"Implementations": [
{
"Male": {
"Gender": "Male"
},
"Female": {
"Gender": "Female"
},
"Country": [
{
"Orientation": "Male",
"Name": ABCD
},
{
"Orientation": "Female",
"Name": EFGH
},
{
"Orientation": "Female",
"Name": IJKL
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
}
]
}
var output = [];
for (k in Implementations.Implementations.Male) {
var temp = [];
for (j in Implementations.Implementations.Male[k]) {
temp.push({
Country: j
});
}
output.push({
"Implementations": k,
Country: temp
});
}
console.log(output);
先感谢您!