注意
このリポジトリは維持されなくなりました。代替案については、以下のセクションを検討してください。
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に2番目の引数を提供できます。今、あなたは以下を設定できます:
id :アイテムのIDフィールドのキー。また、ネストされたプロパティ( "nested.parentId"など)で動作します。デフォルト: "id" 。parentId :アイテムの親のIDフィールドのキー。また、ネストされたプロパティ( "nested.parentId"など)で動作します。デフォルト: "parentId" 。nestedIds :ネストされたIDを有効/無効にするオプション。デフォルト: true 。childrenField :親ノードのすべての子ノードを含むキー。デフォルト: "children"dataField :元のアイテムのすべてのプロパティ/データを含むキー。コンテナが欲しくない場合は、nullに設定します。デフォルト: "data"throwIfOrphans :アイテムの配列に配列に親がない1つ以上のアイテムが含まれている場合、またはアイテムの配列に円形の親/子関係のあるアイテムが含まれている場合、エラーをスローするオプション。このオプションには小さなランタイムペナルティがあるため、デフォルトでは無効になっています。有効にすると、関数は、アイテムアレイには見られなかった括弧を含むエラーをスローします。ツリー内のノードの数が元の配列のノードの数よりも小さい場合、関数はエラーをスローします。無効になると、この機能は孤児や循環関係を無視し、ツリーに追加しません。デフォルト: falserootParentIds :親IDをキーとして、そしてツリーの上部またはルート要素と見なす必要がある値としてtrueオブジェクト。これは、ツリーが完全なツリーのサブセットである場合に役立ちます。つまり、親IDがundefinedの1つ、 nullまたは''の1つであるアイテムはありません。渡す配列は、デフォルト値を置き換えます。 undefinedでnullは常にrootparentidsと見なされます。詳細については、#23を参照してください。デフォルト: {'': true}assign :Spreadオペレーターの代わりに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