从数据中删除叶节点

我正在做一个圆形包装,图形中的节点太多,所以我试图通过删除叶节点来减少节点的数量。


我从 api 获得的数据是一个 json 对象,如下所示:



{

    Children: [], 

    Label: "some str", 

    Value: some int, 

    Properties:[]

}


我正在尝试创建一个循环遍历数据的函数,如果对象没有子对象,则将其删除。这是我正在做的


function removeLeaves(data){

let keys = Object.entries(data);

for(let [name,obj] of keys){

    if(name == "Children"){

        if((<Array<any>>obj).length > 0){

            for(let child of (<Array<any>>obj)){


                removeLeaves(child);

            }

        }

        else{

            data = {}; //delete object

        }

    }


  }

}

但由于数据不是引用类型,因此不会保存更改。谁能帮我这个?我正在尝试做类似于 c# removeLeaves(ref data) 的事情


或者有什么方法可以去除包装方法中的叶子


var pack = data => d3.pack()

    .size([width, height])

    .padding(5)

    (d3.hierarchy(data, d => d.Children)

     //here some kind of filtering

    .sum(d => {            

        return d.Value;

    })

    .sort((a, b) => b.value - a.value));


手掌心
浏览 107回答 2
2回答

紫衣仙女

我会选择一种递归/以编程方式构建所需数据结构的方法,而不是通过删除不需要的属性来改变现有的输入数据......// {Children: [], Label: "some str", Value: some int, Properties:[] }const data = {&nbsp; Label: "root_with_children",&nbsp; Value: 1,&nbsp; Properties: ["foo", "bar"],&nbsp; Children: [{&nbsp; &nbsp; Label: "level_1_without_children",&nbsp; &nbsp; Value: 2,&nbsp; &nbsp; Properties: ["foo", "bar"],&nbsp; &nbsp; Children: []&nbsp; }, {&nbsp; &nbsp; Label: "level_1_with_children",&nbsp; &nbsp; Value: 3,&nbsp; &nbsp; Properties: ["foo", "bar"],&nbsp; &nbsp; Children: [{&nbsp; &nbsp; &nbsp; Label: "level_2_without_children",&nbsp; &nbsp; &nbsp; Value: 4,&nbsp; &nbsp; &nbsp; Properties: ["foo", "bar"],&nbsp; &nbsp; &nbsp; Children: []&nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; Label: "level_2_with_children",&nbsp; &nbsp; &nbsp; Value: 5,&nbsp; &nbsp; &nbsp; Properties: ["foo", "bar"],&nbsp; &nbsp; &nbsp; Children: [{&nbsp; &nbsp; &nbsp; &nbsp; Label: "level_3_without_children",&nbsp; &nbsp; &nbsp; &nbsp; Value: 6,&nbsp; &nbsp; &nbsp; &nbsp; Properties: ["foo", "bar"],&nbsp; &nbsp; &nbsp; &nbsp; Children: []&nbsp; &nbsp; &nbsp; }]&nbsp; &nbsp; }]&nbsp; }]};function isNonEmtyArray(type) {&nbsp; return (Array.isArray(type) && (type.length >= 1));}function collectItemsWithChildrenOnly(list, item) {&nbsp; const { Children } = item;&nbsp; if (isNonEmtyArray(Children)) {&nbsp; &nbsp; const copy = Object.assign({}, item, { Children: [] });&nbsp; &nbsp; list.push(copy);&nbsp; &nbsp; Children.reduce(collectItemsWithChildrenOnly, copy.Children);&nbsp; }&nbsp; return list;}let test;test = [data].reduce(collectItemsWithChildrenOnly, []);console.log('1st run :: test : ', test);test = test.reduce(collectItemsWithChildrenOnly, []);console.log('2nd run :: test : ', test);test = test.reduce(collectItemsWithChildrenOnly, []);console.log('3rd run :: test : ', test);test = test.reduce(collectItemsWithChildrenOnly, []);console.log('4th run :: test : ', test);test = test.reduce(collectItemsWithChildrenOnly, []);console.log('countercheck :: test : ', test);.as-console-wrapper { min-height: 100%!important; top: 0; }

拉莫斯之舞

假设您获得的对象数组的Children属性是对象数组或空 arr 指示它是叶子,您可以使用以下函数的组合删除叶子节点const removeEmptyChildren = obj => obj.Children.length?&nbsp; &nbsp; {...obj,Children:leafRemover(obj.Children)}:&nbsp; &nbsp; undefinedconst leafRemover = arr => arr.filter( e => removeEmptyChildren(e) !== undefined)console.log(leafRemover(data)) // where data is array of objects from the server
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript