从深度嵌套的对象(架构)创建对象路径

Create object paths from a deeply nested object (Schema)(从深度嵌套的对象(架构)创建对象路径)
本文介绍了从深度嵌套的对象(架构)创建对象路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个avro模式,它是一个具有相同(子)结构的深度嵌套对象。看起来是这样的:

{
  "type": "record",
  "namespace": "company.car.v1",
  "name": "CarV1",
  "fields": [
    {
      "name": "plateNumber",
      "type": "string"
    },
    {
      "name": "ownerId",
      "type": "string",
      "keepThisField": "true"
    },
    {
      "name" : "details",
      "keepThisField": "true"
      "type" : {
            "type" : "record",
            "name" : "DetailsV1",
            "fields" : [
              {
                "name": "engine",
                "type": {
                  "type": "record",
                  "name": "EngineV1",
                  "fields": [
                    {
                      "name": "size",
                      "type": "int",
                      "default": 0,
                      "keepThisField": "true"
                    },
                    {
                      "name": "valvesCount",
                      "type": "int",
                      "default": 0
                    }
                  ]
                }
              },
              {
                "name" : "color", 
                "type" : "string", 
                "default" : "NONE"
              },
              {
                "name" : "rimSize",
                "type" : "int", 
                "default" : "NONE"
              }
             ]},
             "default" : {}
   },
    {
      "name": "isBrandNew",
      "type": "boolean"
    }
  ]
}

我希望能够在JavaScript中获得该对象(Schema)的所有对象路径。因此,如果有类似extractPaths(avroSchema)的内容,则上面的示例将返回:

[
   "plateNumber",
   "ownerId",
   "details.engine.size",
   "details.engine.valvesCount",
   "details.color",
   "details.rimSize",
   "isBrandNew"
]

字符串的顺序显然不重要。有人知道如何在JavaScript中实现这一点吗?

推荐答案

使用递归生成器函数生成路径字符串。

var schema = { "type": "record", "namespace": "company.car.v1", "name": "CarV1", "fields": [{ "name": "plateNumber", "type": "string" }, { "name": "ownerId", "type": "string", "keepThisField": "true" }, { "name": "details", "keepThisField": "true", "type": { "type": "record", "name": "DetailsV1", "fields": [{ "name": "engine", "type": { "type": "record", "name": "EngineV1", "fields": [{ "name": "size", "type": "int", "default": 0, "keepThisField": "true" }, { "name": "valvesCount", "type": "int", "default": 0 }] } }, { "name": "color", "type": "string", "default": "NONE" }, { "name": "rimSize", "type": "int", "default": "NONE" }] }, "default": {} }, { "name": "isBrandNew", "type": "boolean" }] }

function* extractPaths(schema, value) {
    for (const { name, type } of schema.fields) {
        let path = value ? `${value}.${name}` : name
        if (typeof type == "object")
            yield* extractPaths(type, path);
        else
            yield path
    }
}
console.log([...extractPaths(schema)]);

这篇关于从深度嵌套的对象(架构)创建对象路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)