arraylist-to-tree

array of list to tree

Usage no npm install needed!

<script type="module">
  import arraylistToTree from 'https://cdn.skypack.dev/arraylist-to-tree';
</script>

README

arraylist-to-tree

A small library that converts Array of list to tree view

Installation

npm install arraylist-to-tree

Usage

const tree = require('arraylistTotree');

const list = [{
    id: 1,
    parentInt: 0
}, {
    id: 2,
    parentInt: 1
}, {
    id: 3,
    parentInt: 1
}, {
    id: 4,
    parentInt: 2
}, {
    id: 5,
    parentInt: 2
}];

Result

[{
"id": 1,
"parentInt": 0,
"children": [
    {
        "id": 2,
        "parentInt": 1,
        "children": [
            {
                "id": 4,
                "parentInt": 2
            }, {
                "id": 5,
                "parentInt": 2
            }
        ]
    }]
}]

Tests

npm test