{"id":26352005,"url":"https://github.com/denq/list-to-tree","last_synced_at":"2025-08-09T00:02:51.923Z","repository":{"id":55658260,"uuid":"41686390","full_name":"DenQ/list-to-tree","owner":"DenQ","description":"Convert list to tree","archived":false,"fork":false,"pushed_at":"2023-01-09T06:54:58.000Z","size":44,"stargazers_count":87,"open_issues_count":11,"forks_count":30,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-06-26T10:11:25.849Z","etag":null,"topics":["algorithm","algorithm-library","converter","data-structures","datastructures","help-tools","list-to-tree","tools","tree","tree-structure","trees"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DenQ.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-31T16:10:54.000Z","updated_at":"2025-05-31T14:18:53.000Z","dependencies_parsed_at":"2023-02-08T09:15:39.943Z","dependency_job_id":null,"html_url":"https://github.com/DenQ/list-to-tree","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/DenQ/list-to-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenQ%2Flist-to-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenQ%2Flist-to-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenQ%2Flist-to-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenQ%2Flist-to-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DenQ","download_url":"https://codeload.github.com/DenQ/list-to-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DenQ%2Flist-to-tree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266076036,"owners_count":23872729,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","algorithm-library","converter","data-structures","datastructures","help-tools","list-to-tree","tools","tree","tree-structure","trees"],"created_at":"2025-03-16T10:36:09.935Z","updated_at":"2025-07-20T06:04:26.657Z","avatar_url":"https://github.com/DenQ.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# list-to-tree\nThis lib is help-tool for convertation list to tree a data structure.\n\n### Attention\n\u003e * Recently I have rewritten the project and now it is based on [IronTree](https://github.com/DenQ/iron-tree) - it allowed to do the project in unix way style and added flexibility. IronTree has a fairly rich interface.\n\u003e* The tree can now be sorted - you only need to pass your sorting method if you are not satisfied with the native sorting.\n\n## Install on npm\n    npm install list-to-tree --save\n\n## Usage\n```js\n    var LTT = require('list-to-tree');\n    var list = [\n    {\n        id: 1,\n        parent: 0\n    }, {\n        id: 2,\n        parent: 1\n    }, {\n        id: 3,\n        parent: 1\n    }, {\n        id: 4,\n        parent: 2\n    }, {\n        id: 5,\n        parent: 2\n    }, {\n        id: 6,\n        parent: 0\n    }, {\n        id: 7,\n        parent: 0\n    }, {\n        id: 8,\n        parent: 7\n    }, {\n        id: 9,\n        parent: 8\n    }, {\n        id: 10,\n        parent: 0\n    }\n    ];\n\n    var ltt = new LTT(list, {\n        key_id: 'id',\n        key_parent: 'parent'\n    });\n    var tree = ltt.GetTree();\n\n    console.log( tree );\n```\n###### Result\n\n    [{\n        \"id\": 1,\n        \"parent\": 0,\n        \"child\": [\n            {\n                \"id\": 2,\n                \"parent\": 1,\n                \"child\": [\n                    {\n                        \"id\": 4,\n                        \"parent\": 2\n                    }, {\n                        \"id\": 5,\n                        \"parent\": 2\n                    }\n                ]\n            },\n            {\n                \"id\": 3,\n                \"parent\": 1\n            }\n        ]\n    }, {\n        \"id\": 6,\n        \"parent\": 0\n    }, {\n        \"id\": 7,\n        \"parent\": 0,\n        \"child\": [\n            {\n                \"id\": 8,\n                \"parent\": 7,\n                \"child\": [\n                    {\n                        \"id\": 9,\n                        \"parent\": 8\n                    }\n                ]\n            }\n        ]\n    }, {\n        \"id\": 10,\n        \"parent\": 0\n    }];\n\n\n# Properties\n* **tree** - This property is `IronTree` type and have methods: add, remove, contains, sort, move, traversal, toJson, etc...\n* **options**\n  * `key_id` (string) Field name for id item. Default: 'id'.\n  * `key_parent` (string) Field name for parent id. Default: 'parent'.\n  * `key_child` (string) Field name for children of item. Default  'child'.\n  * `empty_children` (boolean) Flag for allow empty children property in item. Default: false.\n\n# Methods\n* **constructor(list, options)**\n  * params:\n    * `list` - array list with elements. Like ```{ id: 5: parent: 1 }```.\n    * `options` - optional parameter. Object for describe flags and field names for tree.\n* **.GetTree()** This method will be return json tree\n  * example:\n    ```\n      tree.GetTree()\n    ```\n* **.sort(callback)** The custom sort method\n  * callback(a, b) - a and b have `IronTree\\Node` type and have methods: add, remove, get, set, sort, traversal, etc...\n  * example:\n    ```js\n    function compareById(vector) {\n      return (a, b) =\u003e {\n        const aid = Number(a.get('id'));\n        const bid = Number(b.get('id'));\n        if (aid \u003e bid) {\n          return vector ? 1 : -1;\n        } else if (aid \u003c bid) {\n          return vector ? -1 : 1;\n        } else {\n          return 0\n        }\n      };\n    }\n    ltt.sort(compareById(false));\n    ```\n\n# Testing\nFor run testing, typing on your console\n\n    npm test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenq%2Flist-to-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdenq%2Flist-to-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdenq%2Flist-to-tree/lists"}