
Security News
Deno 2.6 + Socket: Supply Chain Defense In Your CLI
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.
array-to-tree
Advanced tools

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).
$ npm install array-to-tree --save
var arrayToTree = require('array-to-tree');
var data = [{
id: 1,
name: "Portfolio",
parent_id: null
}, {
id: 2,
name: "Web Development",
parent_id: 1
}, {
id: 3,
name: "Recent Works",
parent_id: 2
}, {
id: 4,
name: "About Me",
parent_id: null
}];
arrayToTree(data);
/*
* Output:
*
* Portfolio
* Web Development
* Recent Works
* About Me
*/
var arrayToTree = require('array-to-tree');
var data = [{
_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(data, {
parentProperty: 'parent',
customID: '_id'
});
/*
* Output:
*
* Portfolio
* Web Development
* Recent Works
* About Me
*/
arrayToTree(data, options)Convert a plain array of nodes (with pointers to parent nodes) to a a nested data structure.
data: An array of dataoptions: 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'MIT © Philipp Alferov
FAQs
Convert a plain array of nodes (with pointers to parent nodes) to a tree
The npm package array-to-tree receives a total of 12,697 weekly downloads. As such, array-to-tree popularity was classified as popular.
We found that array-to-tree demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Deno 2.6 introduces deno audit with a new --socket flag that plugs directly into Socket to bring supply chain security checks into the Deno CLI.

Security News
New DoS and source code exposure bugs in React Server Components and Next.js: what’s affected and how to update safely.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.