node.js - 如何编写一个循环遍历深层嵌套 json 模式表单对象的函数?

标签 node.js jsonschema

我正在使用 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/

相关文章:

node.js - 具有联合类型的 ajv 数组

node.js - Post 请求永远不会被发出,只是发送 get

javascript - 在 Node.JS 的 EJS 中检查对象中的未定义属性

javascript - 使用 NodeJ 读取 OSX 标签

node.js - 如何测试在 kubernetes 集群上部署为 pod 的 Nodejs 应用程序?

c++ - 自动将 JSON 数据迁移到最新版本的 JSON 模式

javascript - JSONSchema 和验证子对象属性

javascript - Node.js 监听动态创建的对象上的事件

javascript - 为什么 JSON API 规范建议使用连字符/减号来分隔成员名称中的单词?

json - 将必填字段应用于引用的 JSON 数据架构