# data-tree-util

> tree util

Latest version **1.0.0** (published 2021-01-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install data-tree-util
pnpm add data-tree-util
yarn add data-tree-util
bun add data-tree-util
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-01-09 |
| First published | 2019-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 27.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | changlin |
| Maintainers | changlin |
| Keywords | data-tree-util, treeToArray, treeFromArray, findChildren  |

## Links

- npm: https://www.npmjs.com/package/data-tree-util
- Repository: https://github.com/changlin-cn/data-tree-util
- Homepage: https://github.com/changlin-cn/data-tree-util#readme
- Issues: https://github.com/changlin-cn/data-tree-util/issues
- npm.io page: https://npm.io/package/data-tree-util

## Dependencies (1)

- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.12.5

## Recent versions

- 1.0.0 (latest) — 2021-01-09
- 0.1.5 — 2020-10-15
- 0.1.4 — 2020-08-20
- 0.1.3 — 2020-08-11
- 0.1.2 — 2019-11-05
- 0.1.1 — 2019-10-30
- 0.1.0 — 2019-05-16
- 0.0.2 — 2019-01-17
- 0.0.1 — 2019-01-17
- 0.0.0 — 2019-01-03

## README

# data-tree-util

## Usage
[source code](https://github.com/changlin-cn/data-tree-util/blob/master/src/index.js)
```javascript
npm install data-tree-util --save-dev

const {
    treeFromArray,
    findChildren,
    treeToArray,
    findLeavesFromTree,
    findAncestors,
} = require('data-tree-util');

const data = [
        {
            id: '1',
            parentId: null,
        },

        {
            id: '1-1-1',
            test: '111',
            parentId: '1-1',
        },
        {
            id: '1-2-1',
            parentId: '1-2',
        },
        {
            id: '1-1-2',
            parentId: '1-1',
        },
        {
            id: '1-1-3',
            parentId: '1-1',
        },
        {
            id: '1-1',
            parentId: '1',
        },
        {
            id: '1-2',
            parentId: '1',
        },

        {
            id: '1-2-2',
            parentId: '1-2',
        },
        {
            id: '1-3',
            parentId: '1',
        },
        {
            id: '2',
            parentId: null,
        },
    ];
    treeFromArray(data);
    /*
    =>
    [
      {
        "id": "1",
        "parentId": null,
        "children": [
          {
            "id": "1-1",
            "parentId": "1",
            "children": [
              {
                "id": "1-1-1",
                "test": "111",
                "parentId": "1-1"
              },
              {
                "id": "1-1-2",
                "parentId": "1-1"
              },
              {
                "id": "1-1-3",
                "parentId": "1-1"
              }
            ]
          },
          {
            "id": "1-2",
            "parentId": "1",
            "children": [
              {
                "id": "1-2-1",
                "parentId": "1-2"
              },
              {
                "id": "1-2-2",
                "parentId": "1-2"
              }
            ]
          },
          {
            "id": "1-3",
            "parentId": "1"
          }
        ]
      },
      {
        "id": "2",
        "parentId": null
      }
    ]
    */



   const data2 = [{ id2: '1' }, { id2: '2' }, { id2: '1-2', pid: '1' }];
    treeFromArray(data2, {
        parentIdKey: 'pid',
        idKey: 'id2',
        childrenKey: 'cd',
    });
    /*
    =>
    [
      {
        "id2": "1",
        "cd": [
          {
            "id2": "1-2",
            "pid": "1"
          }
        ]
      },
      {
        "id2": "2"
      }
    ]
    */



    const tree = [
        {
            id: '1',
            children: [
                {
                    id: '1-1',
                    children: [
                        {
                            id: '1-1-1',
                            test: '1-1-1',
                        },
                        {
                            id: '1-1-2',
                            test: '1-1-2',
                        },
                        {
                            id: '1-1-3',
                        },
                    ],
                },
                {
                    id: '1-2',

                    children: [
                        {
                            id: '1-2-1',
                        },
                        {
                            id: '1-2-2',
                        },
                    ],
                },
                {
                    id: '1-3',
                },
            ],
        },
        {
            id: '2',
        },
    ];

    treeToArray(tree)
    /*
    =>
    [ { id: '1' },
      { id: '2' },
      { id: '1-1', parentId: '1' },
      { id: '1-2', parentId: '1' },
      { id: '1-3', parentId: '1' },
      { id: '1-1-1', test: '1-1-1', parentId: '1-1' },
      { id: '1-1-2', test: '1-1-2', parentId: '1-1' },
      { id: '1-1-3', parentId: '1-1' },
      { id: '1-2-1', parentId: '1-2' },
      { id: '1-2-2', parentId: '1-2' } ]
    */






   const tree2 = [
        {
            id: '1',
            children: [
                {
                    id: '1-1',
                    children: [
                        {
                            id: '1-1-1',
                            test: '1-1-1',
                        },
                        {
                            id: '1-1-2',
                            test: '1-1-2',
                        },
                        {
                            id: '1-1-3',
                        },
                    ],
                },
                {
                    id: '1-2',

                    children: [
                        {
                            id: '1-2-1',
                        },
                        {
                            id: '1-2-2',
                        },
                    ],
                },
                {
                    id: '1-3',
                },
            ],
        },
        {
            id: '2',
        },
    ];

    findLeavesFromTree(tree2);
    /*
    =>
    [ { id: '2' },
      { id: '1-3', parentId: '1' },
      { id: '1-1-1', test: '1-1-1', parentId: '1-1' },
      { id: '1-1-2', test: '1-1-2', parentId: '1-1' },
      { id: '1-1-3', parentId: '1-1' },
      { id: '1-2-1', parentId: '1-2' },
      { id: '1-2-2', parentId: '1-2' } ]
    
    */





   const data3 = [
        {
            id: '1',
            parentId: null,
        },

        {
            id: '1-1-1',
            test: '111',
            parentId: '1-1',
        },
        {
            id: '1-2',
            parentId: '1',
        },
        {
            id: '1-2-1',
            parentId: '1-2',
        },
        {
            id: '1-1-2',
            parentId: '1-1',
        },
        {
            id: '1-1-3',
            parentId: '1-1',
        },
        {
            id: '1-1',
            parentId: '1',
        },
    ];
findChildren(data3, '1');

/*
=>
[ { id: '1-2', parentId: '1' },
      { id: '1-1', parentId: '1' },
      { id: '1-2-1', parentId: '1-2' },
      { id: '1-1-1', test: '111', parentId: '1-1' },
      { id: '1-1-2', parentId: '1-1' },
      { id: '1-1-3', parentId: '1-1' } ]

*/
```

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