成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

javascript 遞歸 tree

DrizzleX / 3202人閱讀

let animals = [
    { id: "animals", name: "animals", parent: null },
    { id: "mammals", name: "mammals", parent: "animals" },
    { id: "mammals2", name: "mammals2", parent: "animals" },
    { id: "cats", name: "cats", parent: "mammals" },
    { id: "dogs", name: "dogs", parent: "mammals" },
    { id: "chihuahua", name: "chihuahua", parent: "dogs" },
    { id: "labrador", name: "labrador", parent: "dogs" },
    { id: "persian", name: "persian", name: "animals", parent: "cats" },
    { id: "siamese", name: "siamese", parent: "cats" }
]
let isEmptyObject = (obj) => {
    for (let t in obj)
        return !1;
    return !0
}
let makeTree = (animals, parent) => {
    let node = {}

    let filters = animals.filter(c => c.parent === parent)

    if (filters.length) {
        filters.forEach(c => {

            node[c.id] = c

            n = makeTree(animals, c.id)

            node[c.id]["children"] = []

            if (!isEmptyObject(n)) {
                node[c.id]["children"].push(n)
            }

        })
    }

    return node
}
console.log(
    JSON.stringify(
        makeTree(animals, null), null, 2
    )
)
/**
 {
  "animals": {
    "id": "animals",
    "name": "animals",
    "parent": null,
    "children": [
      {
        "mammals": {
          "id": "mammals",
          "name": "mammals",
          "parent": "animals",
          "children": [
            {
              "cats": {
                "id": "cats",
                "name": "cats",
                "parent": "mammals",
                "children": [
                  {
                    "persian": {
                      "id": "persian",
                      "name": "animals",
                      "parent": "cats",
                      "children": []
                    },
                    "siamese": {
                      "id": "siamese",
                      "name": "siamese",
                      "parent": "cats",
                      "children": []
                    }
                  }
                ]
              },
              "dogs": {
                "id": "dogs",
                "name": "dogs",
                "parent": "mammals",
                "children": [
                  {
                    "chihuahua": {
                      "id": "chihuahua",
                      "name": "chihuahua",
                      "parent": "dogs",
                      "children": []
                    },
                    "labrador": {
                      "id": "labrador",
                      "name": "labrador",
                      "parent": "dogs",
                      "children": []
                    }
                  }
                ]
              }
            }
          ]
        },
        "mammals2": {
          "id": "mammals2",
          "name": "mammals2",
          "parent": "animals",
          "children": []
        }
      }
    ]
  }
}
*/

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/88243.html

相關(guān)文章

  • JavaScript來(lái)學(xué)習(xí)樹(shù)「譯」

    摘要:樹(shù)可謂是開(kāi)發(fā)者最常碰到的數(shù)據(jù)結(jié)構(gòu)之一了要知道整張網(wǎng)頁(yè)就是一棵樹(shù)啊所以我們就來(lái)學(xué)習(xí)樹(shù)這一數(shù)據(jù)結(jié)構(gòu)吧在這篇文章中我們將創(chuàng)建一棵樹(shù)并且用兩種不同的方法來(lái)遍歷它深度優(yōu)先遍歷和寬度廣度優(yōu)先遍歷方法使用借助棧這一數(shù)據(jù)結(jié)構(gòu)來(lái)訪問(wèn)樹(shù)的每個(gè)節(jié)點(diǎn)則借助了隊(duì) 樹(shù)可謂是web開(kāi)發(fā)者最常碰到的數(shù)據(jù)結(jié)構(gòu)之一了. 要知道, 整張網(wǎng)頁(yè)就是一棵DOM樹(shù)啊 (Document Object Model ). 所以我...

    Youngdze 評(píng)論0 收藏0
  • 數(shù)據(jù)結(jié)構(gòu)JavaScript描述(三)

    摘要:有關(guān)算法,數(shù)據(jù)結(jié)構(gòu)的代碼已上傳至算法與數(shù)據(jù)結(jié)構(gòu)。構(gòu)造函數(shù)深度優(yōu)先遍歷廣度優(yōu)先遍歷插入中序遍歷前序遍歷后序遍歷聲明一棵樹(shù)聲明一個(gè)節(jié)點(diǎn)相關(guān)算法深度優(yōu)先遍歷深度優(yōu)先遍歷,先查看左孩子是否存在,若存在,傳入遞歸,否則,再查看右孩子。 這次來(lái)了解一下二叉樹(shù),以及相應(yīng)的算法。以下代碼并非所有都由本人所寫,只是在此分享出來(lái),以便大家學(xué)習(xí)。 有關(guān)javascript算法,數(shù)據(jù)結(jié)構(gòu)的代碼已上傳至 jav...

    張遷 評(píng)論0 收藏0
  • JavaScript實(shí)現(xiàn)無(wú)限級(jí)遞歸樹(shù)

    摘要:需求最近遇到一個(gè)需求,平時(shí)被后臺(tái)慣著直接返回了樹(shù)形結(jié)構(gòu)給到前端,前端對(duì)這種嵌套類型的數(shù)據(jù)如地區(qū)的級(jí)聯(lián)或菜單的樹(shù)形結(jié)構(gòu)省掉了一層處理。當(dāng)然,沒(méi)用好就相當(dāng)于一堆廢鐵,甚至將導(dǎo)致一些不可預(yù)料的結(jié)果。相比起遞歸,我更喜歡這種方法。 需求 最近遇到一個(gè)需求,平時(shí)被后臺(tái)慣著直接返回了樹(shù)形結(jié)構(gòu)給到前端,前端對(duì)這種嵌套類型的數(shù)據(jù)(如地區(qū)的級(jí)聯(lián)或菜單的樹(shù)形結(jié)構(gòu))省掉了一層處理。換了個(gè)后臺(tái)小哥哥返回了扁平...

    atinosun 評(píng)論0 收藏0
  • LeetCode 之 JavaScript 解答第94題 —— 二叉樹(shù)的中序遍歷

    摘要:小鹿題目二叉樹(shù)中序遍歷給定一個(gè)二叉樹(shù),返回它的中序遍歷。通常遞歸的方法解決二叉樹(shù)的遍歷最方便不過(guò),但是我還是喜歡增加點(diǎn)難度,用一般的迭代循環(huán)來(lái)實(shí)現(xiàn)。 Time:2019/4/25Title:Binary Tree Inorder TraversalDifficulty: MediumAuthor:小鹿 題目:Binary Tree Inorder Traversal(二叉樹(shù)中序遍歷...

    Jason 評(píng)論0 收藏0
  • 【譯】JavaScript數(shù)據(jù)結(jié)構(gòu)(4):樹(shù)

    摘要:遍歷樹(shù)是訪問(wèn)樹(shù)的每個(gè)節(jié)點(diǎn)的正式方式。想象一下,我們要將包含奇數(shù)數(shù)據(jù)的任何節(jié)點(diǎn)記錄到控制臺(tái),并使用遍歷樹(shù)中的每個(gè)節(jié)點(diǎn)。第三個(gè)參數(shù),是這個(gè)方法中用來(lái)遍歷樹(shù)的類型。與類似,移除將遍歷樹(shù)以查找包含第二個(gè)參數(shù)的節(jié)點(diǎn),現(xiàn)在為。 翻譯:瘋狂的技術(shù)宅英文:https://code.tutsplus.com/art...說(shuō)明:本文翻譯自系列文章《Data Structures With JavaScri...

    Keagan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<