【发布时间】:2020-06-12 12:10:49
【问题描述】:
我正在使用 JavaScript 和下面显示的 JSON 结构。我需要创建树的修剪版本,以便用户可以选择特定的属性值,并且新树将包含具有该值的所有节点以及具有该值作为后代的任何节点。例如,如果用户选择属性“可以移动”,则新树应包含节点 0、4、2、8、1、14、12 和 17。节点 4、8、14 和 17 具有属性,节点 0、2 , 1 和 12 在其后代之一中具有该属性。任何帮助表示赞赏
var treeData = {
"Node": 0,
"attributes": ["needs water to live"],
"objects": ["corn", "bean", "dog", "reed", "water weeds", "frog", "bream", "fish leech"],
"own_objects": [],
"ObjectCount": "8 | 100%",
"children": [{
"Node": 4,
"attributes": ["can move"],
"objects": ["fish leech", "dog", "frog", "bream"],
"own_objects": ["fish leech"],
"ObjectCount": "4 | 50%",
"children": [{
"Node": 5,
"attributes": ["has limbs"],
"objects": ["bream", "frog", "dog"],
"own_objects": ["bream", "frog", "dog"],
"ObjectCount": "3 | 37%"
}]
}, {
"Node": 3,
"attributes": ["needs chlorophyll"],
"objects": ["bean", "corn", "reed", "water weeds"],
"own_objects": ["bean"],
"ObjectCount": "4 | 50%",
"children": [{
"Node": 6,
"attributes": ["monocotyledon"],
"objects": ["water weeds", "reed", "corn"],
"own_objects": ["water weeds", "reed", "corn"],
"ObjectCount": "3 | 37%"
}]
}, {
"Node": 2,
"attributes": ["lives on land"],
"objects": ["dog", "frog", "corn", "bean", "reed"],
"own_objects": [],
"ObjectCount": "5 | 62%",
"children": [{
"Node": 8,
"attributes": ["can move", "has limbs"],
"objects": ["frog", "dog"],
"own_objects": ["frog"],
"ObjectCount": "2 | 25%",
"children": [{
"Node": 9,
"attributes": ["breast feeds"],
"objects": ["dog"],
"own_objects": ["dog"],
"ObjectCount": "1 | 12%"
}]
}, {
"Node": 7,
"attributes": ["needs chlorophyll"],
"objects": ["corn", "reed", "bean"],
"own_objects": [],
"ObjectCount": "3 | 37%",
"children": [{
"Node": 11,
"attributes": ["monocotyledon"],
"objects": ["reed", "corn"],
"own_objects": ["reed", "corn"],
"ObjectCount": "2 | 25%"
}, {
"Node": 10,
"attributes": ["dicotyledon"],
"objects": ["bean"],
"own_objects": ["bean"],
"ObjectCount": "1 | 12%"
}]
}]
}, {
"Node": 1,
"attributes": ["lives in water"],
"objects": ["bream", "fish leech", "water weeds", "reed", "frog"],
"own_objects": [],
"ObjectCount": "5 | 62%",
"children": [{
"Node": 14,
"attributes": ["can move"],
"objects": ["fish leech", "frog", "bream"],
"own_objects": ["fish leech"],
"ObjectCount": "3 | 37%",
"children": [{
"Node": 15,
"attributes": ["has limbs"],
"objects": ["bream", "frog"],
"own_objects": ["bream", "frog"],
"ObjectCount": "2 | 25%"
}]
}, {
"Node": 13,
"attributes": ["needs chlorophyll", "monocotyledon"],
"objects": ["water weeds", "reed"],
"own_objects": ["water weeds", "reed"],
"ObjectCount": "2 | 25%"
}, {
"Node": 12,
"attributes": ["lives on land"],
"objects": ["frog", "reed"],
"own_objects": [],
"ObjectCount": "2 | 25%",
"children": [{
"Node": 17,
"attributes": ["can move", "has limbs"],
"objects": ["frog"],
"own_objects": ["frog"],
"ObjectCount": "1 | 12%"
}, {
"Node": 16,
"attributes": ["needs chlorophyll", "monocotyledon"],
"objects": ["reed"],
"own_objects": ["reed"],
"ObjectCount": "1 | 12%"
}]
}]
}]
};
【问题讨论】:
标签: javascript json tree pruning