Skip to content

Instantly share code, notes, and snippets.

@ZackFox
Created August 1, 2017 14:03
Show Gist options
  • Save ZackFox/7c8f94cc735e010b9f4a6fd21d1508fe to your computer and use it in GitHub Desktop.
Save ZackFox/7c8f94cc735e010b9f4a6fd21d1508fe to your computer and use it in GitHub Desktop.
function makeTree(list, node) {
const map = {};
const tree = [];
for(let i = 0; i < list.length; i++){
map[list[i].id] = list[i];
map[list[i].id].children = [];
}
for(let i = 1; i < list.length; i++){
item = map[i];
if (item.parent_id !== null) {
map[item.parent_id].children.push(item);
}
else {
tree.push(item);
}
}
console.log(tree);
}
var data2 = [
{
title: "Одежда",
parent_id: null,
id: 1,
},
{
title: "Мужская",
parent_id: 1,
id: 2,
},
{
title: "Костюмы",
parent_id: 2,
id: 3,
},
{
title: "Платья",
parent_id: 7,
id: 4,
},
{
title: "Юбки",
parent_id: 7,
id: 5 ,
},
{
title: "Блузы",
parent_id: 7,
id: 6,
},
{
title: "Женская",
parent_id: 1,
id: 7,
},
{
title: "Брюки",
parent_id: 3,
id: 8,
},
{
title: "Жакеты",
parent_id: 3,
id: 9,
},
{
title: "Вечерние",
parent_id: 4,
id: 10,
},
{
title: "Летние",
parent_id: 4,
id: 11,
}
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment