如何检查特定的对象键是否作为另一个对象键的值存在



要求:

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

例如:

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)。如果您看到JSON,它x映射到类似Node2的(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上的属性。

如果您发布代码,我们可以为您提供更多帮助但是现在,如果你想替换数组之间的值,你可以有很多方法,但这取决于大部分数据和使用它的方法

const updateData = (YourData) => {
for (const [MyNodeName, MyNodeValue] of Object.entries(YourData)) {
if (MyNodeValue.properties) {
for (const [propKey, propValue] of Object.entries(MyNodeValue.properties)) {
if (propValue.mapping && propValue.mapping !== "") {
var updateKeyArr = propValue.mapping.split(".inputs.");
console.log(updateKeyArr)
if (updateKeyArr.length > 0) {
if (YourData[updateKeyArr[0]] &&
YourData[updateKeyArr[0]].properties[updateKeyArr[1]]
) {
YourData[MyNodeName].properties[propKey].mapping = YourData[updateKeyArr[0]].properties[updateKeyArr[1]];
}
}
}
}
}
}
return YourData;
}
console.log(updateData(data))

相关内容

最新更新