{"id":21107898,"url":"https://github.com/alferov/array-to-tree","last_synced_at":"2025-04-05T05:08:57.728Z","repository":{"id":42332226,"uuid":"42969962","full_name":"alferov/array-to-tree","owner":"alferov","description":"Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure","archived":false,"fork":false,"pushed_at":"2022-02-11T11:51:53.000Z","size":320,"stargazers_count":164,"open_issues_count":12,"forks_count":30,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T04:11:15.744Z","etag":null,"topics":["array-helper","array-manipulations","arrays","nodejs","tree","tree-structure"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alferov.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-23T01:01:19.000Z","updated_at":"2024-12-30T09:42:28.000Z","dependencies_parsed_at":"2022-09-08T02:01:35.634Z","dependency_job_id":null,"html_url":"https://github.com/alferov/array-to-tree","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alferov%2Farray-to-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alferov%2Farray-to-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alferov%2Farray-to-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alferov%2Farray-to-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alferov","download_url":"https://codeload.github.com/alferov/array-to-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289429,"owners_count":20914464,"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":["array-helper","array-manipulations","arrays","nodejs","tree","tree-structure"],"created_at":"2024-11-20T00:43:15.705Z","updated_at":"2025-04-05T05:08:57.709Z","avatar_url":"https://github.com/alferov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# array-to-tree\n\n[![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]\n\n![array-to-tree](media/array-to-tree.png)\n\n\u003e Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure.\n\nSolves a problem with conversion of retrieved from a database sets of data to a nested data structure (i.e. navigation tree).\n\n## Installation\n\n```bash\n$ npm install array-to-tree --save\n```\n\n## Usage\n\n```js\nvar arrayToTree = require('array-to-tree');\n\nvar dataOne = [\n  {\n    id: 1,\n    name: 'Portfolio',\n    parent_id: undefined\n  },\n  {\n    id: 2,\n    name: 'Web Development',\n    parent_id: 1\n  },\n  {\n    id: 3,\n    name: 'Recent Works',\n    parent_id: 2\n  },\n  {\n    id: 4,\n    name: 'About Me',\n    parent_id: undefined\n  }\n];\n\narrayToTree(dataOne);\n\n/*\n * Output:\n *\n * Portfolio\n *   Web Development\n *     Recent Works\n * About Me\n */\n\nvar dataTwo = [\n  {\n    _id: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c',\n    name: 'Portfolio',\n    parent: null\n  },\n  {\n    _id: 'ec666030-7f8f-11e3-ae96-0123456789ab',\n    name: 'Web Development',\n    parent: 'ec654ec1-7f8f-11e3-ae96-b385f4bc450c'\n  },\n  {\n    _id: 'ec66fc70-7f8f-11e3-ae96-000000000000',\n    name: 'Recent Works',\n    parent: 'ec666030-7f8f-11e3-ae96-0123456789ab'\n  },\n  {\n    _id: '32a4fbed-676d-47f9-a321-cb2f267e2918',\n    name: 'About Me',\n    parent: null\n  }\n];\n\narrayToTree(dataTwo, {\n  parentProperty: 'parent',\n  customID: '_id'\n});\n\n/*\n * Output:\n *\n * Portfolio\n *   Web Development\n *     Recent Works\n * About Me\n */\n```\n\n## API\n\n### `arrayToTree(data, [options])`\n\nConvert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.\n\n#### Parameters\n\n- **Array** `data`: An array of data\n- **Object** `options`: An object containing the following fields:\n  - `parentProperty` (String): A name of a property where a link to a parent node could be found. Default: 'parent_id'.\n  - `childrenProperty` (String): A name of a property where children nodes are going to be stored. Default: 'children'.\n  - `customID` (String): An unique node identifier. Default: 'id'.\n\n#### Return\n\n- **Array**: Result of transformation\n\n## License\n\nMIT © [Philipp Alferov](https://github.com/alferov)\n\n[travis-url]: https://travis-ci.org/alferov/array-to-tree\n[travis-image]: https://img.shields.io/travis/alferov/array-to-tree.svg?style=flat-square\n[depstat-url]: https://david-dm.org/alferov/array-to-tree\n[depstat-image]: https://david-dm.org/alferov/array-to-tree.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falferov%2Farray-to-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falferov%2Farray-to-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falferov%2Farray-to-tree/lists"}