javascript - 如何检查特定对象键是否作为另一个对象键的值存在?
问题描述
要求:
我必须迭代多个对象,它包含属性。所以我必须迭代每个对象并检查与第一个对象键匹配的属性值(键名是映射)。
例如:
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。对其他人相似
解决方案
你可以这样做:
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
。
推荐阅读
- python - 创建一个熊猫系列的负数箱
- node.js - 我自己做了一个 Pusher.com 的库,但是
- bash - bash:检查两个变量是否都存在或不存在(也就是比较比较结果)
- unity3d - 附加到游戏对象的统一角色
- appcelerator-hyperloop - Titanium Hyperloop 将 ListView 转换为 UIScrollView 以使用 flashScrollIndicators
- marklogic - 如何在 marklogic 服务器中获取所有用户会话?
- permissions - 如何仅授予用户对一个 DNN 门户的管理员权限?
- dart - 如何在我的颤振应用程序中打开一个应用程序而不让它填满整个屏幕?
- ios - 如何根据状态栏中设备的时间更新时间?
- flexbox - 分类图像未垂直对齐