Skip to content

Instantly share code, notes, and snippets.

@wicksome
Last active January 22, 2019 05:19
Show Gist options
  • Save wicksome/aa886511792da336ee756407a9cab2e6 to your computer and use it in GitHub Desktop.
Save wicksome/aa886511792da336ee756407a9cab2e6 to your computer and use it in GitHub Desktop.
create tres structure
// sorted data
var resData = [
{ id: "1", level: 1 },
{ id: "2", level: 1 },
{ id: "2-1", level: 2 },
{ id: "2-1-1", level: 3 },
{ id: "2-2", level: 2 },
{ id: "3", level: 1 },
{ id: "3-1", level: 2 },
{ id: "3-1-1", level: 3 },
{ id: "4", level: 1 },
{ id: "4-1", level: 2 },
{ id: "5", level: 1 },
{ id: "5-1", level: 2 },
{ id: "5-2", level: 2 },
{ id: "5-2-1", level: 3 },
{ id: "5-2-2", level: 3 },
{ id: "5-2-2-1", level: 4 },
{ id: "5-2-2-1-1", level: 5 },
{ id: "5-3", level: 2 },
{ id: "6", level: 1 },
{ id: "7", level: 1 },
{ id: "7-1", level: 2 }
];
var convert = nodes => {
const stack = [];
const result = [];
let current = result;
let depth = 1;
nodes.forEach(node => {
if (node.level === depth) {
// sibling
current.push({ ...node });
} else if (depth < node.level) {
// child
depth++;
current[current.length - 1].child = [{ ...node }];
stack.push(current);
current = current[current.length - 1].child;
} else {
// parent
let loop = depth - node.level;
while (loop--) {
current = stack.pop();
depth--;
}
current.push({ ...node });
}
});
return result;
};
console.log(JSON.stringify(convert(resData), null, 2));
// link: stackoverflow.com/a/18018037/3793078
const data = [
{ id: 1, text: "1", parent: 0 },
{ id: 5, text: "2", parent: 0 },
{ id: 9, text: "3", parent: 0 },
{ id: 10, text: "4", parent: 0 },
{ id: 11, text: "5", parent: 0 },
{ id: 12, text: "6", parent: 0 },
{ id: 2, text: "1-1", parent: 1 },
{ id: 4, text: "1-2", parent: 1 },
{ id: 13, text: "1-3", parent: 1 },
{ id: 3, text: "1-1-1", parent: 2 },
{ id: 6, text: "2-1", parent: 5 },
{ id: 7, text: "2-2", parent: 5 },
{ id: 8, text: "2-1-1", parent: 6 }
];
function list_to_tree(list) {
var map = {};
for (let i = 0; i < list.length; i += 1) {
map[list[i].id] = i; // initialize the map
list[i].children = []; // initialize the children
}
let node, roots = [];
for (let i = 0; i < list.length; i += 1) {
node = list[i];
if (node.parent !== 0) {
// if you have dangling branches check that map[node.parentId] exists
list[map[node.parent]].children.push(node);
} else {
roots.push(node);
}
}
return roots;
}
console.log(data);
console.log("-----------");
console.log(JSON.stringify(list_to_tree(data), null, 2));
@wicksome
Copy link
Author

wicksome commented Dec 19, 2018

tree1.js result:

[{
  "id": "1",
  "level": 1
}, {
  "id": "2",
  "level": 1,
  "child": [{
    "id": "2-1",
    "level": 2,
    "child": [{
      "id": "2-1-1",
      "level": 3
    }]
  }, {
    "id": "2-2",
    "level": 2
  }]
}, {
  "id": "3",
  "level": 1,
  "child": [{
    "id": "3-1",
    "level": 2,
    "child": [{
      "id": "3-1-1",
      "level": 3
    }]
  }]
}, {
  "id": "4",
  "level": 1,
  "child": [{
    "id": "4-1",
    "level": 2
  }]
}, {
  "id": "5",
  "level": 1,
  "child": [{
    "id": "5-1",
    "level": 2
  }, {
    "id": "5-2",
    "level": 2,
    "child": [{
      "id": "5-2-1",
      "level": 3
    }, {
      "id": "5-2-2",
      "level": 3,
      "child": [{
        "id": "5-2-2-1",
        "level": 4,
        "child": [{
            "id": "5-2-2-1-1",
            "level": 5
          }]
      }]
    }]
  }, {
    "id": "5-3",
    "level": 2
  }]
}, {
  "id": "6",
  "level": 1
}, {
  "id": "7",
  "level": 1,
  "child": [{
    "id": "7-1",
    "level": 2
  }]
}]

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