Last active
August 4, 2017 13:02
-
-
Save MisreadableMind/12d7e61a1e9da2e0a1cb9d60caec17ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function drawNestedSetsTree(data, node) { | |
| let map = new Map(); | |
| data.sort((a, b) => { | |
| return a.left - b.left; | |
| }); | |
| data.forEach((item) => { | |
| map.set(item.left, item); | |
| }); | |
| let resultData = { | |
| children: [] | |
| }; | |
| const processLevel = (parent = {}, left) => { | |
| let item = map.get(left); | |
| while (item) { | |
| let child = {title: item.title, children: []}; | |
| parent.children.push(child); | |
| processLevel(child, item.left + 1); | |
| item = map.get(item.right + 1); | |
| } | |
| }; | |
| const renderLevel = (parent = {}, node) => { | |
| if (parent.children.length === 0) return; | |
| const ul = document.createElement('ul'); | |
| node.appendChild(ul); | |
| parent.children.forEach(child => { | |
| const li = document.createElement('li'); | |
| ul.appendChild(li); | |
| li.textContent = child.title; | |
| renderLevel(child, li); | |
| }); | |
| }; | |
| processLevel(resultData, data[0].left); | |
| renderLevel(resultData, node); | |
| } | |
| if (typeof module !== 'undefined') { | |
| module.exports = drawNestedSetsTree; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Приветствую!
Поздравляю, ваш код прошел все наши тесты.
Пожалуйста, заполните форму регистрации на курс:
https://goo.gl/BEq8sP