Giter VIP home page Giter VIP logo

list-to-tree's Introduction

list-to-tree

This lib is help-tool for convertation list to tree a data structure.

Install on bower

bower install list-to-tree --save

Install on npm

npm install list-to-tree --save

Usage

var LTT = require('list-to-tree');
var list = [
{
    id: 1,
    parent: 0
}, {
    id: 2,
    parent: 1
}, {
    id: 3,
    parent: 1
}, {
    id: 4,
    parent: 2
}, {
    id: 5,
    parent: 2
}, {
    id: 6,
    parent: 0
}, {
    id: 7,
    parent: 0
}, {
    id: 8,
    parent: 7
}, {
    id: 9,
    parent: 8
}, {
    id: 10,
    parent: 0
}
];

var ltt = new LTT(list, {
    key_id: 'id',
    key_parent: 'parent'
});
var tree = ltt.GetTree();

console.log( tree );
Result
[{
    "id": 1,
    "parent": 0,
    "child": [
        {
            "id": 2,
            "parent": 1,
            "child": [
                {
                    "id": 4,
                    "parent": 2
                }, {
                    "id": 5,
                    "parent": 2
                }
            ]
        },
        {
            "id": 3,
            "parent": 1
        }
    ]
}, {
    "id": 6,
    "parent": 0
}, {
    "id": 7,
    "parent": 0,
    "child": [
        {
            "id": 8,
            "parent": 7,
            "child": [
                {
                    "id": 9,
                    "parent": 8
                }
            ]
        }
    ]
}, {
    "id": 10,
    "parent": 0
}];

Api

var ltt = new LTT(list, {
    key_id: 'our id key',
    key_parent: 'our parent key',
    key_child: 'our child key'
});

If two param will not have, then:

  • key_id === 'id',
  • key_parent === 'parent'
  • key_child === 'child'

You can be set names this params any words

Testing

Need Jasmine a testing framework

For run testing, typing on your console

jasmine

list-to-tree's People

Contributors

denq avatar grant avatar mucholucho avatar

Watchers

James Cloos avatar Matt Wade avatar

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.