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": [] } } ] } } */
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/88243.html
摘要:樹可謂是開發者最常碰到的數據結構之一了要知道整張網頁就是一棵樹啊所以我們就來學習樹這一數據結構吧在這篇文章中我們將創建一棵樹并且用兩種不同的方法來遍歷它深度優先遍歷和寬度廣度優先遍歷方法使用借助棧這一數據結構來訪問樹的每個節點則借助了隊 樹可謂是web開發者最常碰到的數據結構之一了. 要知道, 整張網頁就是一棵DOM樹啊 (Document Object Model ). 所以我...
摘要:有關算法,數據結構的代碼已上傳至算法與數據結構。構造函數深度優先遍歷廣度優先遍歷插入中序遍歷前序遍歷后序遍歷聲明一棵樹聲明一個節點相關算法深度優先遍歷深度優先遍歷,先查看左孩子是否存在,若存在,傳入遞歸,否則,再查看右孩子。 這次來了解一下二叉樹,以及相應的算法。以下代碼并非所有都由本人所寫,只是在此分享出來,以便大家學習。 有關javascript算法,數據結構的代碼已上傳至 jav...
摘要:需求最近遇到一個需求,平時被后臺慣著直接返回了樹形結構給到前端,前端對這種嵌套類型的數據如地區的級聯或菜單的樹形結構省掉了一層處理。當然,沒用好就相當于一堆廢鐵,甚至將導致一些不可預料的結果。相比起遞歸,我更喜歡這種方法。 需求 最近遇到一個需求,平時被后臺慣著直接返回了樹形結構給到前端,前端對這種嵌套類型的數據(如地區的級聯或菜單的樹形結構)省掉了一層處理。換了個后臺小哥哥返回了扁平...
摘要:小鹿題目二叉樹中序遍歷給定一個二叉樹,返回它的中序遍歷。通常遞歸的方法解決二叉樹的遍歷最方便不過,但是我還是喜歡增加點難度,用一般的迭代循環來實現。 Time:2019/4/25Title:Binary Tree Inorder TraversalDifficulty: MediumAuthor:小鹿 題目:Binary Tree Inorder Traversal(二叉樹中序遍歷...
摘要:遍歷樹是訪問樹的每個節點的正式方式。想象一下,我們要將包含奇數數據的任何節點記錄到控制臺,并使用遍歷樹中的每個節點。第三個參數,是這個方法中用來遍歷樹的類型。與類似,移除將遍歷樹以查找包含第二個參數的節點,現在為。 翻譯:瘋狂的技術宅英文:https://code.tutsplus.com/art...說明:本文翻譯自系列文章《Data Structures With JavaScri...
閱讀 2028·2023-04-26 01:33
閱讀 1660·2023-04-26 00:52
閱讀 1035·2021-11-18 13:14
閱讀 5395·2021-09-26 10:18
閱讀 2901·2021-09-22 15:52
閱讀 1488·2019-08-29 17:15
閱讀 3017·2019-08-29 16:11
閱讀 1039·2019-08-29 16:11