🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

array-to-tree

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-to-tree

Convert a plain array of nodes (with pointers to parent nodes) to a tree

0.5.0
Source
npm
Version published
Weekly downloads
19K
11.95%
Maintainers
1
Weekly downloads
 
Created
Source

array-to-tree

Convert a plain array of nodes (with pointers to parent nodes) to a tree.

Solves a problem with conversion of retrieved from a database sets of data to a nested structure (i.e. navigation tree).

Install

$ npm install --save array-to-tree

Usage

var arrayToTree = require('array-to-tree');

var navigation = [{
  id: 1,
  name: "Portfolio",
  parent_id: null
}, {
  id: 2,
  name: "Web Development",
  parent_id: 1
}, {
  id: 3,
  name: "Recent Works",
  parent_id: 2
}, {
  id: 4,
  name: "About Me",
  parent_id: null
}];

var navigationTree = arrayToTree(navigation);

/*
 * Output:
 * Portfolio
 *   Web Development
 *     Recent Works
 * About Me
 */

License

MIT Philipp Alferov

Keywords

array

FAQs

Package last updated on 23 Sep 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts