![](/img/trans.png)
[英]Sorting hierarchical data in Javascript (Adjacency List/Flat table)
[英]Hierarchical Sorting of Categories in Javascript
我試圖以一種分層的方式對下面給出的類別進行排序,然后將它們輸出到網頁上。 對於一個事實,我知道我需要使用某種形式的遞歸,因為類別可以具有任何深度。 我還希望能夠按字母順序在相同深度級別上對這些類別進行排序,我知道我可以使用someArray.sort()
方法來進行someArray.sort()
。
理想情況下,我將具有類似這樣的結構,每個名稱都是樹中的一個對象,並且term_id是其在數組中的索引:
0 Computer Technology
0 --> Hardware
0 ----> Microprocessors
0 ----> Hard Drives
0 --> Software
0 ----> Firmware & Embedded Systems
0 ----> Operating Systems
0 ----> Web and Internet-Based Applications
0 Uncategorized
到目前為止,我已經瀏覽了網站上的許多問題,但仍無法根據我的需要明確地解決其中的任何一個問題。 到目前為止,這是我到達的地方:
function hierarchicalSort( categories, sorted, parentID ) {
for( var i = 0; i < categories.length; i++ ) {
if( categories[i] === null )
{
continue; // There may be more categories to sort
}
else if( categories[i].parent == parentID ) {
sorted.push( categories[i] ); // Same parent ( same level )
categories[i] = null; // Our "Already Sorted" flag
}
else {
sorted[categories[i].term_id].children = [];
// This category has a different parent
hierarchicalSort( categories, sorted, categories[i].term_id );
// Find this category's children
}
}
}
我的類別包含以下信息:
var categories = [
{
term_id: 1,
name: Uncategorized,
parent: 0,
count: 1
},
{
term_id: 2,
name: Hardware,
parent: 7,
count: 1
},
{
term_id: 3,
name: Software,
parent: 7,
count: 2
},
{
term_id: 7,
name: Computer Tech,
parent: 0,
count: 0
},
{
term_id: 8,
name: Operating Systems,
parent: 3,
count: 1
},
{
term_id: 9,
name: Firmware and Embedded Systems,
parent: 3,
count: 0
},
{
term_id: 10,
name: Web and Internet-Based Applications,
parent: 3,
count: 0
},
{
term_id: 11,
name: Hard Drives,
parent: 7,
count: 3
},
{
term_id: 23,
name: Microprocessors,
parent: 7,
count: 1
}
];
我建議的解決方案:
將類別數組用作隊列,並在while循環運行時檢查每個類別。
/**
* Creates a category tree structure from a list of objects.
*
* @param {array} categories: The list of categories to sort into a hierarchical tree.
* It is mainly accessed as a queue to ensure that no category is missed during each
* generation of the tree level.
*
* @param {array} sorted: The "treeified" structure of categories
* @param {int} parentID: The ID of the parent category
*
* @since 0.2.0
*
* @returns {undefined}
*/
function hierarchicalSort( categories, sorted, parentID )
{
/**
* Term Iterator var i: Goal is to go through each category at least once
*/
var i = categories.length;
// This loop completes a full cycle through categories for each depth level
while( --i >= 0 ) {
// Pull the category out from the sorting "queue"
var category = categories.shift();
if( category.parent == parentID ) {
sorted.push( category ); // Same parent
}
else {
categories.push( category ); // Uhh, this category has a different parent...
// Put it back on the queue for sorting at a later time
}
}
// Keep going until you hit the end of the array
for( var j = 0; typeof sorted[j] !== 'undefined'; j++ ) {
sorted[j].children = [];
/**
* Find this category's children, by importing the remaining queue,
* giving the current sorted category's children property, and the
* current sorted category's term_id
*/
hierarchicalSort( categories, sorted[j].children, sorted[j].term_id );
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.