我正在使用 json 模式表单对象,并且我们有非常深的嵌套对象。为了添加新功能,有时我们需要对每个符合特定条件的子对象“做同样的事情”。
根对象就像
"MainApplicant": {
"type": "object",
"title": "Main Applicant",
"properties": {
"birthDetails": {
"type": "object",
"title": "Birth Details",
"key": "birthDetails",
"properties": {
"dateOfBirth": {
"type": "string",
"title": "Date of Birth",
"key": "dateOfBirth",
"format": "date"
},
"countryOfBirth": {
"type": "string",
"title": "Country of Birth",
"key": "countryOfBirth",
},
},
它可以深入任意层。
现在我正在做我需要做的事情..
Object.keys(properties).forEach(function(key) {
// console.log("dealing with key ",key,properties[key])
if (uischema[key] == undefined) {
uischema[key] = {}
}
if (properties[key].type == "object") {
console.log(key + " is a lvl2 Object!")
Object.keys(properties[key].properties).forEach(function(key2) {
if (uischema[key][key2] == undefined) {
uischema[key][key2] = {}
}
if (properties[key].properties[key2].type == "object") {
// console.log(key2 + " is a lvl3 Object!",properties[key].properties[key2].properties,uischema[key])
Object.keys(properties[key].properties[key2].properties).forEach(function(key3) {
if (uischema[key][key2][key3] == undefined) {
uischema[key][key2][key3] = {}
}
if (properties[key].properties[key2].properties[key3].type == "object") {
本质上,手动循环遍历每一层。在这种情况下,我们只需将 undefined 切换为空对象。
它 super 蹩脚,但我不知道如何为它编写循环函数。
差点就爱上了一个允许在 javascript 中进行 goto 的笑话库!
最佳答案
我使用了递归。这是代码:
var i=0
var properties={ "MainApplicant": {
"type": "object",
"title": "Main Applicant",
"properties": {
"birthDetails": {
"type": "object",
"title": "Birth Details",
"key": "birthDetails",
"properties": {
"dateOfBirth": {
"type": "string",
"title": "Date of Birth",
"key": "dateOfBirth",
"format": "date"
},
"countryOfBirth": {
"type": "string",
"title": "Country of Birth",
"key": "countryOfBirth",
},
}
}
}
}
}
function driller(data){
Object.keys(data).forEach(key=>{
//console.log(JSON.stringify(data[key]));
if(typeof(data[key])==='object'){
console.log(JSON.stringify(data[key]));
driller(data[key])
}
})
}
driller(properties)
这是输出:
E:\Nodetest>node server.js
{"type":"object","title":"Main Applicant","properties":{"birthDetails":{"type":"object","title":"Birth Details","key":"birthDetails","properties":{"dateOfBirth":{"type":"string","title":"Date of Birth","key":"dateOfBirth","format":"date"},"countryOfBirth":{"type":"string","title":"Country of Birth","key":"countryOfBirth"}}}}}
{"birthDetails":{"type":"object","title":"Birth Details","key":"birthDetails","properties":{"dateOfBirth":{"type":"string","title":"Date of Birth","key":"dateOfBirth","format":"date"},"countryOfBirth":{"type":"string","title":"Country of Birth","key":"countryOfBirth"}}}}
{"type":"object","title":"Birth Details","key":"birthDetails","properties":{"dateOfBirth":{"type":"string","title":"Date of Birth","key":"dateOfBirth","format":"date"},"countryOfBirth":{"type":"string","title":"Country of Birth","key":"countryOfBirth"}}}
{"dateOfBirth":{"type":"string","title":"Date of Birth","key":"dateOfBirth","format":"date"},"countryOfBirth":{"type":"string","title":"Country of Birth","key":"countryOfBirth"}}
{"type":"string","title":"Date of Birth","key":"dateOfBirth","format":"date"}
{"type":"string","title":"Country of Birth","key":"countryOfBirth"}
关于node.js - 如何编写一个循环遍历深层嵌套 json 模式表单对象的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53626756/