【问题标题】:Prune JSON tree with JavaScript使用 JavaScript 修剪 JSON 树
【发布时间】: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


    【解决方案1】:

    我想我已经找到了一个解决方案,所以我把它贴在这里以防它对其他人有用,尽管我想这是一个非常具体的要求。

     function hasAttributeOrDescendantAttribute(tree) {
        if (tree.attributes) {
            if (tree.attributes.some(attr => attr == $("#attributeDropdown option:selected").text() ))
                return true;
        }
    
        if (tree.children) {
            if (tree.children.some(hasAttributeOrDescendantAttribute)) return true;
    
        }
        return false;
    }
    
    function prune(tree, result) {
    
        result.Node = tree.Node;
        result.attributes = tree.attributes;
        result.objects = tree.objects;
        result.ObjectCount = tree.ObjectCount;
        result.own_objects = tree.own_objects;
    
        if (tree.children) {
    
            result.children = [];
    
            for (child of tree.children) {
    
                if (hasAttributeOrDescendantAttribute(child)) {
                    result.children.push({});
                    prune(child, result.children[result.children.length - 1]);
                }
            }
        }
        return result;
    }
    
    treeData = prune(treeData, {});
    

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 2012-02-09
      • 2019-05-22
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多