array-to-tree-js

1.0.4 • Public • Published

array-to-tree-js

simple create nested tree from array

Installation

$ npm i array-to-tree-js -P

Usage

import to_tree from 'array-to-tree-js';
 
let dataOne = [{
  id: 1,
  name: "Portfolio",
  parent_id: undefined // or 0
}, {
  id: 2,
  name: "Web Development",
  parent_id: 1
}, {
  id: 3,
  name: "Recent Works",
  parent_id: 2
}, {
  id: 4,
  name: "About Me",
  parent_id: 0
}];
 
 console.log(to_tree(dataOne, {id_parent: 'parent_id', ID: 'id'}));
 
/*
 * Output:
 *
 * Portfolio
 *   Web Development
 *     Recent Works
 * About Me
 */
 

API

array-to-tree-js(data, [options])

Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.

Parameters

  • Array data: An array of data
  • Object options: An object containing the following fields:
    • id_parent (String): A name of a property where a link to a parent node could be found. Default: 'id_parent'
    • ID (String): An unique node identifier. Default: 'id_group'

Return

  • Array: Result of transformation

License

MIT © Serhiy Popaduk

Readme

Keywords

Package Sidebar

Install

npm i array-to-tree-js

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

3.15 kB

Total Files

5

Last publish

Collaborators

  • serhiyp