# @pansy/array-to-tree

> 将普通的数组（带有指向父节点的指针）转换为嵌套的数据结构

Latest version **0.3.0** (published 2021-11-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install @pansy/array-to-tree
pnpm add @pansy/array-to-tree
yarn add @pansy/array-to-tree
bun add @pansy/array-to-tree
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2021-11-25 |
| First published | 2021-11-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 30.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | wangxingkang |

## Links

- npm: https://www.npmjs.com/package/@pansy/array-to-tree
- npm.io page: https://npm.io/package/@pansy/array-to-tree

## Dependencies (2)

- [@pansy/is-nil](https://npm.io/package/@pansy/is-nil.md) 0.5.0
- [@pansy/is-function](https://npm.io/package/@pansy/is-function.md) 0.5.0

## Recent versions

- 0.3.0 (latest) — 2021-11-25
- 0.2.0 — 2021-11-24

## README

<h1 align="center">@pansy/array-to-tree</h1>

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

## 📦 安装

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

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

```

## 🔨 使用

- 使用parentId模式进行转换

```ts
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模式进行转换

```ts
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) 在转换之前处理每项的数据

### 返回

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

```

---
_Source: https://npm.io/package/@pansy/array-to-tree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
