<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<dl>
<dt>
<dd></dd>
</dt>
</dl>
</body>
</html>
<script>
var obj = [{
"tid": "8",
"vid": "2",
"name": "Finance and insurance",
"description": "",
"format": "plain_text",
"weight": "11",
"parent": "0"
}, {
"tid": "29",
"vid": "2",
"name": "Astronomy",
"description": "",
"format": "plain_text",
"weight": "6",
"parent": "11"
}, {
"tid": "16",
"vid": "2",
"name": "Chemistry",
"description": "",
"format": "plain_text",
"weight": "5",
"parent": "11"
}, {
"tid": "40",
"vid": "2",
"name": "Small business",
"description": "",
"format": "plain_text",
"weight": "4",
"parent": "26"
}, {
"tid": "15",
"vid": "2",
"name": "Biology",
"description": "",
"format": "plain_text",
"weight": "4",
"parent": "11"
}, {
"tid": "21",
"vid": "2",
"name": "Sculpting",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "17"
}, {
"tid": "34",
"vid": "2",
"name": "Other",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "19"
}, {
"tid": "25",
"vid": "2",
"name": "Life insurance",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "20"
}, {
"tid": "45",
"vid": "2",
"name": "Jurisprudence",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "36"
}, {
"tid": "19",
"vid": "2",
"name": "Education",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "0"
}, {
"tid": "14",
"vid": "2",
"name": "Corporate",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "26"
}, {
"tid": "30",
"vid": "2",
"name": "Calculus",
"description": "",
"format": "plain_text",
"weight": "3",
"parent": "11"
}, {
"tid": "35",
"vid": "2",
"name": "Toefl",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "34"
}, {
"tid": "36",
"vid": "2",
"name": "Social Studies",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "19"
}, {
"tid": "32",
"vid": "2",
"name": "Retirement",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "26"
}, {
"tid": "43",
"vid": "2",
"name": "Property insurance",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "20"
}, {
"tid": "38",
"vid": "2",
"name": "Physics",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "11"
}, {
"tid": "18",
"vid": "2",
"name": "Other",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "8"
}, {
"tid": "42",
"vid": "2",
"name": "Guitar",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "17"
}, {
"tid": "33",
"vid": "2",
"name": "Geography",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "36"
}, {
"tid": "24",
"vid": "2",
"name": "Analytics",
"description": "",
"format": "plain_text",
"weight": "2",
"parent": "18"
}, {
"tid": "37",
"vid": "2",
"name": "Tutoring",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "11"
}, {
"tid": "10",
"vid": "2",
"name": "Tax return",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "26"
}, {
"tid": "39",
"vid": "2",
"name": "Stock markets",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "18"
}, {
"tid": "31",
"vid": "2",
"name": "Piano",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "17"
}, {
"tid": "13",
"vid": "2",
"name": "Javascript",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "34"
}, {
"tid": "20",
"vid": "2",
"name": "Insurance",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "8"
}, {
"tid": "28",
"vid": "2",
"name": "Economy",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "36"
}, {
"tid": "12",
"vid": "2",
"name": "Auto insurance",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "20"
}, {
"tid": "17",
"vid": "2",
"name": "Art",
"description": "",
"format": "plain_text",
"weight": "1",
"parent": "19"
}, {
"tid": "11",
"vid": "2",
"name": "Science",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "19"
}, {
"tid": "46",
"vid": "2",
"name": "Programming",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "34"
}, {
"tid": "41",
"vid": "2",
"name": "Math",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "11"
}, {
"tid": "44",
"vid": "2",
"name": "Investments",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "26"
}, {
"tid": "27",
"vid": "2",
"name": "History",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "36"
}, {
"tid": "22",
"vid": "2",
"name": "Health insurance",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "20"
}, {
"tid": "26",
"vid": "2",
"name": "Finance",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "8"
}, {
"tid": "23",
"vid": "2",
"name": "Drawing",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "17"
}, {
"tid": "9",
"vid": "2",
"name": "Business planning",
"description": "",
"format": "plain_text",
"weight": "0",
"parent": "18"
}]
function toTree(data) {
// 刪除 所有 children,以防止多次呼叫
data.forEach(function(item) {
delete item.children;
});
// 將資料儲存為 以 tid 為 KEY 的 map 索引資料列
// console.log(data)
var map = {};
data.forEach(function(item) {
map[item.tid] = item;
});
// console.log(map)
var val = [];
data.forEach(function(item) {
// 以當前遍歷項,的parent,去map物件中找到索引的tid
var parent = map[item.parent];
// 那麼說明此項不在頂級當中,那麼需要把此項新增到,他對應的父級中
if (parent) {
(parent.children || (parent.children = [])).push(item);
} else {
// 如果沒有在map中找到對應的索引tid,那麼直接把 當前的item新增到 val結果集中,作為頂級
val.push(item);
}
});
return val;
}
function compare(obj1, obj2) {
var val1 = Number(obj1.weight);
var val2 = Number(obj2.weight);
if (val1 < val2) {
return -1;
} else if (val1 > val2) {
return 1;
} else {
return 0;
}
}
let datalist = toTree(obj)
datalist.sort(compare).forEach(a=>{
a.children.sort(compare).forEach(b =>{
b.children.sort(compare)
})
})
console.log(datalist)
</script>
//需要根據自己實際需要改