首页 > 解决方案 > 如何在javascript中对json数据应用递归函数

问题描述

我在 js 中有这样的验证模式

var data = [
  {
    id: {
      label: "",
      tests: [
        {
          name: "required",
          params: "",
        },
      ],
    },
  },
  {
    name: {
      fields: {
        firstName: {
          label: "",
          tests: [
            {
              name: "required",
              params: "",
            },
          ],
        },
        lastName: {
          label: "",
          tests: [
            {
              name: "required",
              params: "",
            },
          ],
        },
        midName: {
          label: "",
          tests: [],
        },
      },
      tests: [],
    },
  },
  {
    email: {
      label: "",
      tests: [
        {
          name: "required",
          params: "",
        },
      ],
    },
  },
  {
    city: {
      label: "",
      tests: [],
    },
  },
  {
    details: {
      fields: {
        age: {
          label: "",
          tests: [
            {
              name: "max",
              params: {
                max: 18,
              },
            },
          ],
        },
        tests: [],
      },
    },
  },
];

我只想要数组中键的名称,它具有像这个 var selected = [“id”,“name”,“email”] 这样的必填字段。为此,想要创建一个动态函数,例如如果数据具有键:“测试”“必需”,它将返回名称,如果数据具有键:“字段”,它将再次检查“测试”。

标签: javascriptarraysjsonrecursionfilter

解决方案


在此处输入图像描述

var data = [
  {
    id: {
      label: "",
      tests: [
        {
          name: "required",
          params: "",
        },
      ],
    },
  },
  {
    name: {
      fields: {
        firstName: {
          label: "",
          tests: [
            {
              name: "required",
              params: "",
            },
          ],
        },
        lastName: {
          label: "",
          tests: [
            {
              name: "required",
              params: "",
            },
          ],
        },
        midName: {
          label: "",
          tests: [],
        },
      },
      tests: [],
    },
  },
  {
    email: {
      label: "",
      tests: [
        {
          name: "required",
          params: "",
        },
      ],
    },
  },
  {
    city: {
      label: "",
      tests: [],
    },
  },
  {
    details: {
      fields: {
        age: {
          label: "",
          tests: [
            {
              name: "max",
              params: {
                max: 18,
              },
            },
          ],
        },
        tests: [],
      },
    },
  },
]
console.clear()
const keys = data.map(x => recursive(Object.keys(x)[0], x)).filter(x => x !== undefined)
function recursive (name, x) {
    for (let key in x) {
    if (x[key].tests !== undefined && typeof x[key].tests === "object") {
      const find = x[key].tests.find(y => y.name === "required")
      if (find) return name
    } 
    if (x[key].fields !== undefined) {
      for (let y in x[key].fields) {
        if (y !== "tests") {
                console.log(y)
          const v = recursive(parent, x[key].fields)
          if (v !== undefined) return name
        }
      }
    }
  }
}
console.log("KEYS", keys)

require就像您说的那样,如果我们在现场只有一次,则在孩子中进行递归搜索name。只是您需要记住数据的名称并在最后进行过滤,因为我不返回名称,因此undefined如果我没有找到这个特定的字符串字段则返回。


推荐阅读