Skip to content

Instantly share code, notes, and snippets.

@MisreadableMind
Last active August 4, 2017 13:02
Show Gist options
  • Select an option

  • Save MisreadableMind/12d7e61a1e9da2e0a1cb9d60caec17ea to your computer and use it in GitHub Desktop.

Select an option

Save MisreadableMind/12d7e61a1e9da2e0a1cb9d60caec17ea to your computer and use it in GitHub Desktop.
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;
}
@jsninjabot
Copy link

Приветствую!
Поздравляю, ваш код прошел все наши тесты.
Пожалуйста, заполните форму регистрации на курс:
https://goo.gl/BEq8sP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment