[英]Finding optimal nodes whose presence satisfies nested JSON logic
下面是 JSON,我用来生成以下 UI:
{
"Text" : "1. Parent group"
"Logic" : "all of the following",
"Id":"1" //some unique id
"children": [
{
"Text' : "1.1 Level Child group"
"Logic' : "1 or more of the following",
"Id':"1.1" //some unique id,
"children" : [
{
"Text' : "1.1.1 Level Child group"
"Logic' : "all of the following",
"Id':"1.1.1" //some unique id,
"children": [
{
"Text' : "1.1.1.1 Level Child"
"Id':"1.1.1.1" //some unique id,
},
{
"Text' : "1.1.1.2 Level Child"
"Id':"1.1.1.2" //some unique id,
"Logic" : "one or more of the following",
"children" : [
{
"Text": "1.1.1.2.1 Level Child",
"Id": "1.1.1.2.1"
},
{
"Text": "1.1.1.2.2 Level Child",
"Id": "1.1.1.2.2"
},
]
}
]
},
{
"Text' : "1.1.2 Level Child group"
"Id':"1.1.2" //some unique id,
},
{
"Text' : "1.1.3 Level Child group"
"Id':"1.1.3" //some unique id,
},
{
"Text' : "1.1.4 Level Child group"
"Logic' : "one or more of the following",
"Id':"1.1.4" //some unique id,
"children": [
{
"Text' : "1.1.4.1 Level Child"
"Id':"1.1.4.1" //some unique id,
},
{
"Text' : "1.1.4.2 Level Child"
"Id':"1.1.4.2" //some unique id,
}
]
},
{
"Text' : "1.1.5 Level Child group"
"Logic' : "all of the following",
"Id':"1.1.5" //some unique id,
"children": [
{
"Text' : "1.1.5.1 Level Child"
"Id':"1.1.5.1" //some unique id,
},
{
"Text' : "1.1.5.2 Level Child"
"Id':"1.1.5.2" //some unique id,
}
]
}
]
},
{
"Text" : "1.2 Level Child"
"Id":"1.2" //some unique id
}
]
}
1. Parent group (all of the following) 1.1 Level Child group (1 or more ) 1.1.1 Level Child group (all of the following) 1.1.1.1 Level Child // selectable 1.1.1.2 Level Child // selectable 1.1.2 Level Child // selectable 1.1.3 Level Child // selectable 1.1.4 Level Child group (one or more of the following) 1.1.4.1 Level Child // selectable 1.1.4.2 Level Child // selectable 1.1.5 Level Child group (all of the following) 1.1.5.1 Level Child // selectable 1.1.5.2 Level Child // selectable 1.2 Level Child // selectable
节点工作逻辑:
用户只能选择没有子节点的节点。
Node is implicitly selected if the Logic is "all of the following" if and only if all its nested children are selected
如果逻辑是“n 个或多个以下”,则隐式选择节点,前提是其“n”个嵌套子节点被选中
场景:如果用户选择1.1.2 (它隐式满足1.1 )和1.2那么“1.父组”是“遇见”,所以它被选中(使整个组遇见);
场景:如果用户选择1.1.1.1 ,则用户必须选择1.1.1.2以便1.1.1为“Met”,选择1.2将使整个组为“Met”。
实际需要的是在“未满足父组”时找到最佳 id:
如果选择任何节点和最顶层的父组仍然没有“满足”。 然后在单击按钮时,我需要找到并返回最佳节点 ID,其存在将使“父组”成为“Met”。
注意:在找到最佳节点 ID 时,如果选择了任何子组,我们应该重视子组。
例如:
如果用户只选择了1.2 ,那么让“父组”见面的最快方法是选择1.1.2 。 但是如果1.1.1选择了任何子节点,那么我应该优先考虑1.1.1节点,因此我需要返回以下 ID:[ 1.1.1.2, 1.1.1, 1.1 ]
不确定什么类型的算法,我应该使用来解决这个问题。
这是通过查看约束的简短方法,这些约束现在在用q: 'some'
或q: 'every'
提到的数据集中。
function select(node, selected) { function iter(node) { if (!node.children) return [node.Id]; var temp = node.children.map(iter); if (node.q === 'every') return [...temp.flat(), node.Id]; var filtered = temp.filter(a => a.some(v => selected.includes(v))); if (filtered.length) return [...filtered.flat(), node.Id]; return [...temp.reduce((a, b) => b.length < a.length ? b : a), node.Id]; } return iter(node).filter(v => !selected.includes(v)); } var data = { q: 'every', Text: "1. Parent group", Logic: "all of the following", Id: "1", children: [{ q: 'some', Text: "1.1 Level Child group", Logic: "1 or more of the following", Id: "1.1", children: [{ q: 'every', Text: "1.1.1 Level Child group", Logic: "all of the following", Id: "1.1.1", children: [{ Text: "1.1.1.1 Level Child", Id: "1.1.1.1" }, { Text: "1.1.1.2 Level Child", Id: "1.1.1.2" }] }, { Text: "1.1.2 Level Child group", Id: "1.1.2" }, { Text: "1.1.3 Level Child group", Id: "1.1.3" }, { q: 'some', Text: "1.1.4 Level Child group", Logic: "one or more of the following", Id: "1.1.4", children: [{ Text: "1.1.4.1 Level Child", Id: "1.1.4.1" }, { Text: "1.1.4.2 Level Child", Id: "1.1.4.2" }] }, { q: 'every', Text: "1.1.5 Level Child group", Logic: "all of the following", Id: "1.1.5", children: [{ Text: "1.1.5.1 Level Child", Id: "1.1.5.1" }, { Text: "1.1.5.2 Level Child", Id: "1.1.5.1" }] }] }, { Text: "1.2 Level Child", Id: "1.2" }] }; console.log(select(data, ["1.2"])); console.log(select(data, ["1.2", "1.1.1.1"])); console.log(select(data, ["1.1.1.1"]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.