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
摘要:樹(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 ). 所以我...
摘要:有關(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...
摘要:需求最近遇到一個(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)小哥哥返回了扁平...
摘要:小鹿題目二叉樹(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ù)中序遍歷...
摘要:遍歷樹(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...
閱讀 2042·2023-04-26 01:33
閱讀 1669·2023-04-26 00:52
閱讀 1052·2021-11-18 13:14
閱讀 5466·2021-09-26 10:18
閱讀 2919·2021-09-22 15:52
閱讀 1498·2019-08-29 17:15
閱讀 3028·2019-08-29 16:11
閱讀 1046·2019-08-29 16:11