首页 > 解决方案 > 如何检查特定对象键是否作为另一个对象键的值存在?

问题描述

要求:

我必须迭代多个对象,它包含属性。所以我必须迭代每个对象并检查与第一个对象键匹配的属性值(键名是映射)。

例如:

Node1, Node2, Node3

JSON: 
 {
  "Node1": {
    "type": "object",
    "properties": {
      "x": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "y": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node2": {
    "type": "object",
    "properties": {
      "body": {
        "type": "string",
        "mapping": "Node1.inputs.x",
        "checked": false,
        "defChecked": true
      },
      "subject": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "toemail": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "body_placeholders": {
        "type": "string",
        "mapping": "",
        "checked": true,
        "defChecked": true
      },
      "subject_placeholders": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node3": {
    "type": "object",
    "properties": {
      "email": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "Node2.inputs.toemail",
        "isSelected": true,
        "defChecked": true
      },
      "firstname": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      },
      "lastname": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "Node2.inputs.body_placeholders",
        "isSelected": true,
        "defChecked": true
      },
      "id": {
        "type": "uuid",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      }
    },
    "defChecked": true
  }
}

输出:

{
  "Node1": {
    "type": "object",
    "properties": {
      "y": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node2": {
    "type": "object",
    "properties": {
      "subject": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
     
      "subject_placeholders": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node3": {
    "type": "object",
    "properties": {
      "firstname": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      },
      
      "id": {
        "type": "uuid",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      }
    },
    "defChecked": true
  }
}

对于上面的 JSON - Node1 有一个属性'x' 并且这个属性映射或没有另一个对象属性(Node2 和 Node3)。如果您看到它 x 与 Node2 映射的 JSON,例如 (Node1.inputs.x)

如果匹配,那么我必须从 json 中删除对象 Node1.x。对其他人相似

标签: javascriptangulartypescript

解决方案


你可以这样做:

let input = {
  "Node1": {
    "type": "object",
    "properties": {
      "x": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "y": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node2": {
    "type": "object",
    "properties": {
      "body": {
        "type": "string",
        "mapping": "Node1.inputs.x",
        "checked": false,
        "defChecked": true
      },
      "subject": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "toemail": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      },
      "body_placeholders": {
        "type": "string",
        "mapping": "",
        "checked": true,
        "defChecked": true
      },
      "subject_placeholders": {
        "type": "string",
        "mapping": "",
        "checked": false,
        "defChecked": true
      }
    },
    "defChecked": true
  },
  "Node3": {
    "type": "object",
    "properties": {
      "email": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "Node2.inputs.toemail",
        "isSelected": true,
        "defChecked": true
      },
      "firstname": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      },
      "lastname": {
        "type": "text",
        "description": null,
        "default": null,
        "mapping": "Node2.inputs.body_placeholders",
        "isSelected": true,
        "defChecked": true
      },
      "id": {
        "type": "uuid",
        "description": null,
        "default": null,
        "mapping": "",
        "isSelected": true,
        "defChecked": true
      }
    },
    "defChecked": true
  }
};

for (let i = 1; i < 3; i++) {
   Object.entries(input["Node" + (i + 1)].properties).forEach(prop => {
      if (prop[1].mapping.includes("Node" + i)) {
        let attrToCancel = prop[1].mapping.split('.')[prop[1].mapping.split('.').length -1];
     
        delete input["Node" + i].properties[attrToCancel];
        delete input["Node" + (i + 1)].properties[prop[0]];
      }
   });
}

console.log(input)

获取Object.entries输入,然后拦截mapping属性,读取它并取消上一个属性Node


推荐阅读