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