# tree-with-array

> Conversion between tree data and array data

Latest version **1.0.1** (published 2020-07-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install tree-with-array
pnpm add tree-with-array
yarn add tree-with-array
bun add tree-with-array
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-07-29 |
| First published | 2020-07-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 43.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | LoryHuang |
| Maintainers | huangzm |
| Keywords | tree, array, list, collection, list to tree, collection to tree, tree to list, tree to collection |

## Links

- npm: https://www.npmjs.com/package/tree-with-array
- Repository: https://github.com/hzmming/tree-with-array
- Homepage: https://github.com/hzmming/tree-with-array#readme
- Issues: https://github.com/hzmming/tree-with-array/issues
- npm.io page: https://npm.io/package/tree-with-array

## Recent versions

- 1.0.1 (latest) — 2020-07-29
- 1.0.0 — 2020-07-29

## README

# tree-with-array

Conversion between tree data and array data

## Install

```shell
npm i tree-with-array
```

## Usage

**arrayToTree**

```javascript
const { arrayToTree } = require('tree-with-array');
const list = [
  {
    id: '0'
  },
  {
    id: '0-1',
    parentId: '0'
  },
  {
    id: '0-1-1',
    parentId: '0-1'
  },
  {
    id: '0-2',
    parentId: '0'
  },
  {
    id: '1'
  }
];
const tree = arrayToTree(list);
console.log(tree);
/*
  [
    {
      id: '0',
      children: [
        {
          id: '0-1',
          parentId: '0',
          children: [
            {
              id: '0-1-1',
              parentId: '0-1'
            }
          ]
        },
        {
          id: '0-2',
          parentId: '0'
        }
      ]
    },
    {
      id: '1'
    }
  ];
*/
```

**treeToArray**

```javascript
const { treeToArray } = require('tree-with-array');
const tree = [
  {
    id: '0',
    children: [
      {
        id: '0-1',
        children: [
          {
            id: '0-1-1'
          }
        ]
      },
      {
        id: '0-2'
      }
    ]
  },
  {
    id: '1'
  }
];
const list = treeToArray(tree);
console.log(list);
/* 
  [
    {
      id: '0',
      children: [
        {
          id: '0-1',
          children: [
            {
              id: '0-1-1'
            }
          ]
        },
        {
          id: '0-2'
        }
      ]
    },
    {
      id: '0-1',
      children: [
        {
          id: '0-1-1'
        }
      ]
    },
    {
      id: '0-1-1'
    },
    {
      id: '0-2'
    },
    {
      id: '1'
    }
  ];
 */
```

## Api

`arrayToTree(list, [options])`

```javascript
arrayToTree(list, {
  idKey,
  pIdKey
  // ...
});
```

**options**

| name       | description                                                                                                                                                                                                                            | type     | accepted values      | default              |
| ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------------- | -------------------- |
| idKey      | the node's attribute to save node's unique identifier.<br />support chain calls.                                                                                                                                                       | string   | —                    | id                   |
| pIdKey     | the node's attribute to save its parent node's unique identifier.<br />support chain calls.                                                                                                                                            | string   | —                    | parentId             |
| isKeyChain | whether to enable the chain call of idKey and pIdKey                                                                                                                                                                                   | boolean  | —                    | true                 |
| detached   | whether to preserve the node which specifies parent's identifier but the parent node is non-existent.<br />if set true, the node keep as root node.<br />if set false, the node will be abandoned and output error message to console. | boolean  | —                    | true                 |
| sortKey    | the node's attribute to save node's sort value.                                                                                                                                                                                        | string   | —                    | ''                   |
| order      | specify the sort type.<br />works when `sortKey` is specified.                                                                                                                                                                         | string   | ascending/descending | ascending            |
| traverse   | traverse all nodes.<br />parent is null when it has no parent.                                                                                                                                                                         | function | —                    | (node, parent) => {} |

`treeToArray(tree, [options])`

```javascript
treeToArray(tree, {
  childrenKey
});
```

**options**

| name        | description                                                | type   | accepted values | default  |
| ----------- | ---------------------------------------------------------- | ------ | --------------- | -------- |
| childrenKey | specify which node attribute is used as the node's subtree | string | —               | children |

## Test

```shell
npm run test
```

## License

MIT

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