Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
list-to-tree
Advanced tools
Readme
This lib is help-tool for convertation list to tree a data structure.
- Recently I have rewritten the project and now it is based on IronTree - it allowed to do the project in unix way style and added flexibility. IronTree has a fairly rich interface.
- The tree can now be sorted - you only need to pass your sorting method if you are not satisfied with the native sorting.
npm install list-to-tree --save
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 );
[{
"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
}];
IronTree
type and have methods: add, remove, contains, sort, move, traversal, toJson, etc...key_id
(string) Field name for id item. Default: 'id'.key_parent
(string) Field name for parent id. Default: 'parent'.key_child
(string) Field name for children of item. Default 'child'.empty_children
(boolean) Flag for allow empty children property in item. Default: false.list
- array list with elements. Like { id: 5: parent: 1 }
.options
- optional parameter. Object for describe flags and field names for tree. tree.GetTree()
IronTree\Node
type and have methods: add, remove, get, set, sort, traversal, etc...function compareById(vector) {
return (a, b) => {
const aid = Number(a.get('id'));
const bid = Number(b.get('id'));
if (aid > bid) {
return vector ? 1 : -1;
} else if (aid < bid) {
return vector ? -1 : 1;
} else {
return 0
}
};
}
ltt.sort(compareById(false));
For run testing, typing on your console
npm test
FAQs
List to tree data structure
The npm package list-to-tree receives a total of 591 weekly downloads. As such, list-to-tree popularity was classified as not popular.
We found that list-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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.