Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

list2tree

Nelayah38MIT0.0.6TypeScript support: included

It converts the list structure to the tree structure.

list, tree, function

readme

list2tree

It converts the list structure to the tree structure.

Install

npm install list2tree --save

Usage

Demo 01

import list2tree from 'list2tree';

const getTreeData = list2tree({
    idKey: 'id',
    parentIdKey: 'parentId'
});
const data = getTreeData([
    {id: 'A1', parentId: null, name: 'Title_A1'},
    {id: 'A2', parentId: 'A1', name: 'Title_A2'},
    {id: 'A3', parentId: null, name: 'Title_A3'}
]);
console.log(data);

Output

[
  {
    "id": "A1",
    "parentId": null,
    "name": "Title_A1",
    "children": [
      {
        "id": "A2",
        "parentId": "A1",
        "name": "Title_A2"
      }
    ]
  },
  {
    "id": "A3",
    "parentId": null,
    "name": "Title_A3"
  }
]

Demo 02

import list2tree from 'list2tree';

const getTreeData = list2tree({
    idKey: 'id',
    parentIdKey: 'parentId',
    newKey: {
        key: 'id',
        value: 'id',
        title: 'name'
    }
});
const data = getTreeData([
    {id: 'A1', parentId: null, name: 'Title_A1'},
    {id: 'A2', parentId: 'A1', name: 'Title_A2'},
    {id: 'A3', parentId: null, name: 'Title_A3'}
]);
console.log(data);

Output

[
  {
    "id": "A1",
    "parentId": null,
    "name": "Title_A1",
    "key": "A1",
    "value": "A1",
    "title": "Title_A1",
    "children": [
      {
        "id": "A2",
        "parentId": "A1",
        "name": "Title_A2",
        "key": "A2",
        "value": "A2",
        "title": "Title_A2"
      }
    ]
  },
  {
    "id": "A3",
    "parentId": null,
    "name": "Title_A3",
    "key": "A3",
    "value": "A3",
    "title": "Title_A3"
  }
]

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.0.6 (2019-06-18)

0.0.5 (2019-06-17)

0.0.4 (2019-06-17)

0.0.3 (2019-06-17)

Performance Improvements

  • remove namekey fields, add newkey fields (a8cf96b)

0.0.2 (2019-06-17)