【发布时间】:2019-12-01 20:08:13
【问题描述】:
原始json数据:
{
"UniversalOne": "",
"CommonOne": "",
"Implementations": [
{
"BirthDate": "",
"UniqueTraits": "",
"Male": {
"Gender": "Male",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": ""
},
"Female": {
"Gender": "Female",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": ""
},
"Country": [
{
"Orientation": "Male",
"Name": "ABCD",
"County": "East"
},
{
"Orientation": "Male",
"Name": "ABCD",
"County": "West"
},
{
"Orientation": "Female",
"Name": "EFGH",
"County": "East"
},
{
"Orientation": "Female",
"Name": "EFGH",
"County": "West"
},
{
"Orientation": "Female",
"Name": "IJKL"
}
],
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
],
"Boy": [
{
"AgeGroup": "A",
"Id": 1,
"MaternalName": "",
"PaternalName": ""
},
{
"AgeGroup": "B",
"Id": 2,
"MaternalName": "",
"PaternalName": ""
},
{
"AgeGroup": "C",
"Id": 3,
"MaternalName": "",
"PaternalName": ""
}
]
}
],
"PersonalityTraits": [
{
"Type": "Positive"
},
{
"Type": "Negative"
}
],
"UniversalTwo": "",
"CommonTwo": "",
"EatingHabits": {
"Type": "Excessive"
},
"ReadingHabits": {
"Type": "Fast"
},
"FitnessHabits": {},
"UniversalThree": "",
"CommonThree": ""
}
预期的 json 响应:
{
"UniversalOne": "",
"CommonOne": "",
"Implementations": [
{
"BirthDate": "",
"UniqueTraits": "",
"Male": {
"Gender": "Male",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": "",
"Country": [
{
"Orientation": "Male",
"Name": "ABCD"
}
],
"EastCounty": {
"Orientation": "Male",
"Name": "ABCD",
"County": "East"
},
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
},
"Female": {
"Gender": "Female",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": "",
"Country": [
{
"Orientation": "Female",
"Name": "EFGH"
},
{
"Orientation": "Female",
"Name": "IJKL"
}
],
"EastCounty": {
"Orientation": "Female",
"Name": "EFGH",
"County": "East"
},
"State": [
{
"Address": "XYZ Street",
"ZipCode": "US"
}
]
},
"Girl": [
{
"AgeGroup": "A",
"identification": [
{
"Number": 1,
"MaternalName": "",
"PaternalName": ""
}
]
},
{
"AgeGroup": "B",
"identification": [
{
"Number": 1,
"MaternalName": "",
"PaternalName": ""
}
]
},
{
"AgeGroup": "C",
"identification": [
{
"Number": 1,
"MaternalName": "",
"PaternalName": ""
}
]
}
]
}
],
"PersonalityTraits": [
{
"Type": "Positive"
},
{
"Type": "Negative"
}
],
"UniversalTwo": "",
"CommonTwo": "",
"EatingHabits": {
"Type": "Excessive"
},
"ReadingHabits": {
"Type": "Fast"
},
"FitnessHabits": {},
"UniversalThree": "",
"CommonThree": ""
}
问题:
我有三个具体问题:
1) 如何保留“男性”和“女性”正下方以及“男性”之前的属性?在我运行我的程序后,这些属性不会显示在我的响应中。
我想保留像
这样的属性"BirthDate":"",
"UniqueTraits": "" AND
"Gender": "Male",
"PlaceOfBirth": "",
"Weight": "",
"Height": "",
"EyeColor": ""
与我的原始和预期的 json 数据完全相同。
2) 我如何在男性和女性的 Country[] 之后添加另一个 EastCounty{},基于“County”:East 和 Orientation?请参考原始和预期的 json 以供参考。
3) 我如何将原始 json 中的 Boy[] 重构为新结构,正如预期 json 响应中的 Girl[] 所示?注意 Boy[] 中的“Id”更改为 Girl 中的“Number”。因此,如果“AgeGroup”中的任何一个中有多个“标识”,那么每个记录的“Number”都会按顺序更改。
当前节目:
function modifyImplementations(Implementations) {
var finalResult = [];
for (var i = 0; i < Implementations.Implementations.length; i++) {
var currentImplementation = Implementations.Implementations[i];
var targetObj = {
"Male": {
"Gender": "Male",
"Country": [],
"State": currentImplementation.State
},
"Female": {
"Gender": "Female",
"Country": [],
"State": currentImplementation.State
}
};
for (var j = 0; j < currentImplementation.Country.length; j++) {
var currentCountry = currentImplementation.Country[j];
if (currentCountry.Orientation === 'Male') {
targetObj.Male.Country.push(currentCountry);
} else if (currentCountry.Orientation === 'Female') {
targetObj.Female.Country.push(currentCountry);
}
}
finalResult.push(targetObj);
}
return finalResult
}
var x = Object.assign({}, Implementations);
x.Implementations = modifyImplementations(Implementations);
console.log(JSON.stringify(x));
【问题讨论】:
-
正如您所问的,如果要保留大量属性,那么在您创建 targetObj 而不是创建新对象的地方将 currentImplementation 克隆到一个新对象中并制作对其进行更改,例如添加国家/州/县,最后从克隆的 obj 中删除国家/州数组并将所有这些返回到 finalResult,这样您就只会在需要的字段上工作!
-
感谢您的回复,我无法在控制台中看到克隆的对象。 for (var i = 0; i
-
不是 var originalObject = Object.assign({}, currentApplication),改成: var originalObject = Object.assign({}, currentImplementation) 也可以直接赋值: let originalObject = currentImplementation;确保此时 :: var x = Object.assign({}, Implementations); x.Implementations = modifyImplementations(Implementations);控制台.log(JSON.stringify(x)); Node.Js 等待 x.Implementations = modifyImplementations(Implementations);在打印 console.log 之前完成,因为 Node.Js 是异步的,请确保检查!!
-
我已经更新了到目前为止的内容,因为我无法在评论框中添加内容。如何验证克隆的对象是否被正确复制?
-
好的建议,如果你使用vscode作为你的IDE,然后尝试使用quokka扩展,这对代码的运行时分析非常有帮助,你只需要打开quokka选项卡(cmd +shift+p) 将打开一个新的 js 文件,然后粘贴您的代码并确保没有错误,使用少量 console.logs 进行快速检查!
标签: javascript node.js json node-modules