0.3.0 • Published 2 years ago

@pansy/array-to-tree v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

将普通的数组(带有指向父节点的指针)转换为嵌套的数据结构,支持方便的查找到任意节点的数据

📦 安装

// npm
npm install @pansy/array-to-tree --save

// yarn
yarn add @pansy/array-to-tree

🔨 使用

  • 使用parentId模式进行转换
import { arrayToTree } from '@pansy/array-to-tree';

const source = [
  { id: '001', parentId: '' },
  { id: '002', parentId: '' },
  { id: '001001', parentId: '001' },
  { id: '001001001', parentId: '001001' },
  { id: '001002', parentId: '001' },
];

arrayToTree(source).treeData;

// 结果如下
[
  {
    id: '001',
    parentId: '',
    parentIds: [],
    children: [
      {
        id: '001001',
        parentId: '001',
        parentIds: ['001'],
        children: [
          {
            id: '001001001',
            parentId: '001001',
            parentIds: ['001', '001001']
          },
        ]
      },
      {
        id: '001002',
        parentId: '001',
        parentIds: ['001']
      },
    ],
  },
  {
    id: '002',
    parentId: '',
    parentIds: []
  },
];
  • 使用parentIds模式进行转换
import { arrayToTree } from '@pansy/array-to-tree';

const source = [
  { id: '001', parentIds: [] },
  { id: '002', parentIds: [] },
  { id: '001001', parentIds: ['001'] },
  { id: '001001001', parentIds: ['001', '001001'] },
  { id: '001002', parentIds: ['001'] },
];

arrayToTree(source).treeData;

// 转换结果如下
[
  {
    id: '001',
    parentIds: [],
    children: [
      {
        id: '001001',
        parentIds: ['001'],
        children: [
          { id: '001001001', parentIds: ['001', '001001'] },
        ]
      },
      { id: '001002', parentIds: ['001'] },
    ],
  },
  { id: '002', parentIds: [] },
];

API

arrayToTree(data, [options])

参数

  • Array data: 需要转换的数组
  • Object options: 包含以下字段的对象:
    • rootId (string | number) 根节点的值,默认: ''
    • fieldNames (object)
      • id (string) 自定义节点 id 的字段,默认: id
      • parentId (string) 自定义节点 parentId 的字段,默认: parentId
      • parentIds (string) 自定义节点 parentIds 的字段,默认: parentIds
      • children (string) 自定义节点 children 的字段,默认: children
    • mode (string) 创建树的模式,默认: parentId
    • transformItem(function) 在转换之前处理每项的数据

返回

{
  // 转换后的数据
  treeData: array[];
  // 树每个节点的以 id 为 key 的对象
  treeMap: object;
}