第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從數(shù)據(jù)中刪除葉節(jié)點

從數(shù)據(jù)中刪除葉節(jié)點

手掌心 2022-10-27 14:44:13
我正在做一個圓形包裝,圖形中的節(jié)點太多,所以我試圖通過刪除葉節(jié)點來減少節(jié)點的數(shù)量。我從 api 獲得的數(shù)據(jù)是一個 json 對象,如下所示:{    Children: [],     Label: "some str",     Value: some int,     Properties:[]}我正在嘗試創(chuàng)建一個循環(huán)遍歷數(shù)據(jù)的函數(shù),如果對象沒有子對象,則將其刪除。這是我正在做的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        }    }  }}但由于數(shù)據(jù)不是引用類型,因此不會保存更改。誰能幫我這個?我正在嘗試做類似于 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));
查看完整描述

2 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

我會選擇一種遞歸/以編程方式構建所需數(shù)據(jù)結構的方法,而不是通過刪除不需要的屬性來改變現(xiàn)有的輸入數(shù)據(jù)......


// {Children: [], Label: "some str", Value: some int, Properties:[] }


const data = {

  Label: "root_with_children",

  Value: 1,

  Properties: ["foo", "bar"],

  Children: [{


    Label: "level_1_without_children",

    Value: 2,

    Properties: ["foo", "bar"],

    Children: []

  }, {

    Label: "level_1_with_children",

    Value: 3,

    Properties: ["foo", "bar"],

    Children: [{


      Label: "level_2_without_children",

      Value: 4,

      Properties: ["foo", "bar"],

      Children: []

    }, {

      Label: "level_2_with_children",

      Value: 5,

      Properties: ["foo", "bar"],

      Children: [{


        Label: "level_3_without_children",

        Value: 6,

        Properties: ["foo", "bar"],

        Children: []

      }]

    }]

  }]

};


function isNonEmtyArray(type) {

  return (Array.isArray(type) && (type.length >= 1));

}


function collectItemsWithChildrenOnly(list, item) {

  const { Children } = item;

  if (isNonEmtyArray(Children)) {


    const copy = Object.assign({}, item, { Children: [] });

    list.push(copy);


    Children.reduce(collectItemsWithChildrenOnly, copy.Children);

  }

  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; }


查看完整回答
反對 回復 2022-10-27
?
拉莫斯之舞

TA貢獻1820條經驗 獲得超10個贊

假設您獲得的對象數(shù)組的Children屬性是對象數(shù)組或空 arr 指示它是葉子,您可以使用以下函數(shù)的組合刪除葉子節(jié)點


const removeEmptyChildren = obj => obj.Children.length

?

    {...obj,Children:leafRemover(obj.Children)}

:

    undefined



const leafRemover = arr => arr.filter( e => removeEmptyChildren(e) !== undefined)

console.log(leafRemover(data)) // where data is array of objects from the server


查看完整回答
反對 回復 2022-10-27
  • 2 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號