performant array to tree
v1.11.0
警告
該存儲庫不再維護。考慮以下部分以獲取替代方案。
將帶有ID和父ID的項目數組以表演方式(時間複雜度O(n) )轉換為嵌套樹。在瀏覽器和節點中運行。
其他軟件包具有更嚴格的假設,或者不如性能,因為它們經常使用嵌套環或遞歸。例如:
O-Unflatten需要訂購輸入,以使父母的節點總是出現在孩子麵前。 Un-Flatten-Tree使用2個嵌套環(時間複雜性O(n^2) )。
此實現不需要輸入數組中的任何項目,而是關注運行時性能。它是4個不同軟件包中最快的,您可以在此處找到基準。它使用索引和單個循環(時間複雜性O(n) )。它的靈感來自於Stackoverflow上的討論。
yarn add performant-array-to-tree
或使用NPM
npm install --save performant-array-to-tree
const tree = arrayToTree ( [
{ id : "4" , parentId : null , custom : "abc" } ,
{ id : "31" , parentId : "4" , custom : "12" } ,
{ id : "1941" , parentId : "418" , custom : "de" } ,
{ id : "1" , parentId : "418" , custom : "ZZZz" } ,
{ id : "418" , parentId : null , custom : "ü" } ,
] ) ;這會導致以下數組:
[
{
data : { id : "4" , parentId : null , custom : "abc" } ,
children : [
{ data : { id : "31" , parentId : "4" , custom : "12" } , children : [ ] } ,
] ,
} ,
{
data : { id : "418" , parentId : null , custom : "ü" } ,
children : [
{ data : { id : "1941" , parentId : "418" , custom : "de" } , children : [ ] } ,
{ data : { id : "1" , parentId : "418" , custom : "ZZZz" } , children : [ ] } ,
] ,
} ,
] ; 您可以為帶有配置選項的ArrayTotree提供第二個參數。現在,您可以設置以下內容:
id :項目ID字段的鍵。還可以使用嵌套屬性(例如"nested.parentId" )。默認值: "id" 。parentId :父母ID字段的鍵。還可以使用嵌套屬性(例如"nested.parentId" )。默認值: "parentId" 。nestedIds :啟用/禁用嵌套ID的選項。默認值: true 。childrenField :鑰匙將包含父節點的所有子節點。默認: "children"dataField :將包含原始項目的所有屬性/數據的密鑰。如果您不想要容器,請設置為空。默認值: "data"throwIfOrphans :如果物品數組包含一個或多個在陣列中沒有父母的項目,或者項目數組包含具有圓形父母/子女關係的項目,則可以進行錯誤。此選項的運行時罰款很小,因此默認情況下是禁用的。啟用後,該函數將丟棄一個錯誤,其中包含項目數組中未找到的pardenID,或者在僅循環項目關係的情況下,一個通用錯誤。如果樹中的節點的數量小於原始數組中的節點數量,則該功能將引發錯誤。禁用時,該功能只會忽略孤兒和循環關係,而不會將它們添加到樹上。默認值: falserootParentIds :將父ID作為密鑰和true作為對像作為對象,應視為樹的頂部或根元素。當您的樹是完整樹的子集時,這很有用,這意味著沒有一個物品的父ID是undefined , null或'' 。您傳遞的數組將替換默認值。 undefined和null總是被認為是rootparentids。有關更多詳細信息,請參見#23。默認值: {'': true}assign :啟用Object.assign的選項,而不是傳播操作員在dataField為null時在樹中創建一個項目。如果您的項目具有應維護的原型,這將很有用。如果啟用且dataField為null ,則將使用原始節點項目,並將分配children屬性,並在該字段上調用任何設置器。如果dataField並非null ,則此選項無效,因為原始節點將在新對象的dataField下使用。如果您不確定是否需要啟用此功能,則可以將其禁用。默認值: false例子:
const tree = arrayToTree (
[
{ num : "4" , ref : null , custom : "abc" } ,
{ num : "31" , ref : "4" , custom : "12" } ,
{ num : "1941" , ref : "418" , custom : "de" } ,
{ num : "1" , ref : "418" , custom : "ZZZz" } ,
{ num : "418" , ref : null , custom : "ü" } ,
] ,
{ id : "num" , parentId : "ref" , childrenField : "nodes" }
) ;生產的:
[
{
data : { num : "4" , ref : null , custom : "abc" } ,
nodes : [ { data : { num : "31" , ref : "4" , custom : "12" } , nodes : [ ] } ] ,
} ,
{
data : { num : "418" , ref : null , custom : "ü" } ,
nodes : [
{ data : { num : "1941" , ref : "418" , custom : "de" } , nodes : [ ] } ,
{ data : { num : "1" , ref : "418" , custom : "ZZZz" } , nodes : [ ] } ,
] ,
} ,
] ;沒有數據字段的示例:
const tree = arrayToTree (
[
{ id : "4" , parentId : null , custom : "abc" } ,
{ id : "31" , parentId : "4" , custom : "12" } ,
{ id : "1941" , parentId : "418" , custom : "de" } ,
{ id : "1" , parentId : "418" , custom : "ZZZz" } ,
{ id : "418" , parentId : null , custom : "ü" } ,
] ,
{ dataField : null }
) ;生產的:
[
{
id : "4" ,
parentId : null ,
custom : "abc" ,
children : [ { id : "31" , parentId : "4" , custom : "12" , children : [ ] } ] ,
} ,
{
id : "418" ,
parentId : null ,
custom : "ü" ,
children : [
{ id : "1941" , parentId : "418" , custom : "de" , children : [ ] } ,
{ id : "1" , parentId : "418" , custom : "ZZZz" , children : [ ] } ,
] ,
} ,
] ;具有嵌套ID/parentid屬性的示例:
const tree = arrayToTree (
[
{ num : { id : "4" } , parent : { parentId : null } , custom : "abc" } ,
{ num : { id : "31" } , parent : { parentId : "4" } , custom : "12" } ,
] ,
{ id : "num.id" , parentId : "parent.parentId" }
) ;生產的:
[
{
data : { num : { id : "4" } , parent : { parentId : null } , custom : "abc" } ,
children : [
{
data : { num : { id : "31" } , parent : { parentId : "4" } , custom : "12" } ,
children : [ ] ,
} ,
] ,
} ,
] ; 該項目包括類型,只需像往常一樣導入模塊:
import { arrayToTree } from "performant-array-to-tree" ;
const tree = arrayToTree ( array ) ; yarn version創建一個新版本npm login npm publish --access public將其發佈到NPM