如何检查嵌套对象的值是否已存在于另一个对象中?

本垒打

我的 angular 应用程序中有一个函数,我必须在其中检查表单的属性并检查哪些属性具有“required”= true 属性,然后检查用户之前是否已经输入了此信息。通过此验证,如果相应的用户信息已存在,我将跳过该表单,否则我将显示该表单。

更新

我已经通过几次“foreach”检查它们是否具有值,但我被困在了这一点上,我不知道如何解决我需要做的事情。

功能如下


      const initForm = getInitForm();
      const user = getUser();
      const dataNotFilled = [];

      initialForm.props.forEach((prop) => {
        if (prop.required) {
          const nestedObjectData = {};

          Object.keys(user).forEach((initKey) => {

            const value = user[initKey];

            if (initKey === prop.name && value === null) {

              dataNotFilled.push(value);

            } else if (typeof value === 'object') {

              Object.assign(nestedObjectData, value);

              Object.keys(nestedObjectData).forEach((key) => {

                const nestedValue = nestedObjectData[key];

                if (key === prop.name && nestedValue === null) {

                  dataNotFilled.push(nestedValue);

                }

              });

            }

          });

          if (dataNotFilled.length) {
            browserHistory.push(ADD_USER_DATA_PATH);
          }

        }

      });

   


这是我收到的表格所需数据的对象


{
  "label": "city",
  "name": "city",
  "type": "text",
  "parent": "primaryAddress",
  "validations": [
    {
      "message": "error",
      "value": {
        "pattern": "^\\d{2}-\\d{3}$|\\d{5}"
      },
      "rule": "regex"
    },
    {
      "message": "required",
      "value": true,
      "rule": "required"
    }
  ],
  "required": true,
  "services": []
}

这是用户的对象


{
  "name": "John",
  "surname": "Martin",
  "email": null,
  "address": null,
  "phone": {
    "home": null,
    "mobile": null
  },
  "address": {
    "code": "null",
    "address": null,
    "city": "Kansas"
  }
}

如果信息已经存在,如何比较两个对象的属性以跳过表单?提前感谢大家的时间和帮助。

罗宾·迪克霍夫

我认为你让这太复杂了。为什么不简单地做这样的事情:

//Fill the form with your initial object, if any.
this.form = formBuilder.group({
    name: [{value: initialObject.name, disabled: initialObject.name !== null}] // Or '' or undefined
    surname: [{value: initialObject.surname, disabled: initialObject.surname !== null}]
})


// Other logic.

if(form.valid){
    // Form is instantly valid so skip it.
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何检查对象属性是否也存在于另一个对象中?

如何检查一个 json 对象数组中的 id 是否存在于另一个 json 对象嵌套数组中?

如何检查一个JavaScript对象中的值是否存在于另一个JavaScript对象中?

如何使用JS检查对象是否存在于另一个对象中

检查键是否存在于另一个对象中

检查对象值数组是否存在于另一个数组中,但长度不同

如何判断对象的所有值是否存在于另一个对象中

如何检查值是否存在于另一个熊猫数据框中?

使用 lodash 过滤对象数组并检查对象属性值是否存在于另一个数组中

jQuery如何检查一组对象是否存在于另一个数组对象中

reactjs如何过滤/检查数组中的任何对象是否存在于另一个对象数组中?

检查对象是否存在于另一个模型外键字段中

检查值是否存在于选择查询中的另一个表中

查找列表的对象是否存在于另一个列表中

如何检查 csv 文件中的值是否存在于另一个文件中

检查一个对象中的项目是否存在于另一个对象中(使用下划线)

遍历一个IQueriable(对象A)并检查属性是否存在于另一个IQueriable(对象B)中

检查一个表中的值组合是否存在于另一个表中

如何使用SQL检查一个表中存在的json值数组是否存在于另一个表中?

访问一个类的对象以查看其是否存在于另一个类中

检查数组值是否存在于另一个包含逗号分隔值的数组值中

如何检查一个元素是否存在于另一个元素在Jquery中?

如何检查一个 XML 元素是否存在于 nodejs 的另一个元素中?

蜂巢:如何检查一个数组中的值是否存在于另一个数组中?

检查所有数组值是否存在于另一个数组值中

FirebaseRecyclerAdapter 检查 Id 是否存在于另一个表中

检查列表是否存在于另一个列表中

忽略对象中存在于JavaScript另一个数组中的键

雪花。如何检查子数组是否存在于另一个数组中?