Created
August 1, 2017 14:03
-
-
Save ZackFox/7c8f94cc735e010b9f4a6fd21d1508fe to your computer and use it in GitHub Desktop.
This file contains 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 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