Git Product home page Git Product logo

array-to-tree's Introduction

这个数组转为树形格式的函数性能比直接在递归里查找性能好上一倍,应用了原地算法

array-to-tree

Build Status Dependency Status

array-to-tree

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

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

Installation

$ npm install array-to-tree --save

Usage

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

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

arrayToTree(dataOne);

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

var dataTwo = [{
  _id: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c',
  name: "Portfolio",
  parent: null
}, {
  _id: 'ec666030-7f8f-11e3-ae96-0123456789ab',
  name: "Web Development",
  parent: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c'
}, {
  _id: 'ec66fc70-7f8f-11e3-ae96-000000000000',
  name: "Recent Works",
  parent: 'ec666030-7f8f-11e3-ae96-0123456789ab'
}, {
  _id: '32a4fbed-676d-47f9-a321-cb2f267e2918',
  name: "About Me",
  parent: null
}];

arrayToTree(dataTwo, {
  parentProperty: 'parent',
  customID: '_id'
});

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

API

arrayToTree(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:
    • parentProperty (String): A name of a property where a link to a parent node could be found. Default: 'parent_id'
    • customID (String): An unique node identifier. Default: 'id'

Return

  • Array: Result of transformation

License

MIT © Philipp Alferov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.