alferov / array-to-tree Goto Github PK
View Code? Open in Web Editor NEWConvert a plain array of nodes (with pointers to parent nodes) to a nested data structure
License: MIT License
Convert a plain array of nodes (with pointers to parent nodes) to a nested data structure
License: MIT License
I've been trying to get this to work with a nested parent_id
but I can't seem to find the solution.
Say I have:
[
{
"id": "1",
"attributes": {
"name": "Parent",
"parent_id": null,
}
},
{
"id": "2",
"attributes": {
"name": "Child One",
"parent_id": 1,
}
},
{
"id": "3",
"attributes": {
"name": "Child Two",
"parent_id": 1,
}
},
]
Will arrayToTree()
work in this situation?
arrayToTree(dataTwo, {
parentProperty: 'parent',
customID: '_id',
each:(node)=>{
// here i can �do some things. like choice the right node to an other list
}
});
this options we can choice some node but no need to deep traverse the whole tree.
I build trees of objects that shouldn't be cloned, because I want to keep reference. Moreover, cloning messes up my objects that have custom serialization functions.
I will stick to version 3.1.0 until this problem is addressed.
How can I import this module in Angular2 app and use it there ?
Here is my problem in details:
https://stackoverflow.com/questions/55847044/how-to-import-npm-module-in-angular-application-that-exports-function-and-name
hi guys, he's the issue I'm facing using parentProperty options.
var data2 =[
{
id: 1,
created_at: '2020-05-09T16:13:04.896Z',
folder_id: 1,
'Filess.name': 'MyFirstLesson'
},
{
id: 2,
created_at: '2020-05-11T16:13:04.896Z',
folder_id: 2,
'Filess.name': 'MyFirstLesson'
},
{
id: 3,
created_at: '2020-05-11T16:13:04.896Z',
folder_id: 3,
'Filess.name': 'MyFirstLesson'
},
{
id: 4,
created_at: '2020-05-09T16:13:04.896Z',
folder_id: 1,
'Filess.name': 'Meeting1'
},
{
id: 5,
created_at: '2020-05-16T16:13:04.896Z',
folder_id: 1,
'Filess.name': 'SetupRoom'
},
{
id: 6,
created_at: '2020-05-17T16:13:04.896Z',
folder_id: 1,
'Filess.name': 'SetupRoom'
},
{
id: 7,
created_at: '2020-05-17T16:13:04.896Z',
folder_id: 1,
'Filess.name': 'SetupRoom'
},
{
id: 8,
created_at: '2020-05-20T15:26:36.656Z',
folder_id: 1,
'Filess.name': 'Annotation 2020-04-08 135240.jpg'
}
]
arrayToTree(data2, {parentProperty: 'folder_id})
returns empty array
@alferov
https://github.com/alferov/array-to-tree/blob/master/index.js#L19
you just change the passed array by add children
property, and the array has been changed forever.
I mean you can't change the arguments passed in, you should keep the function is pureable
this bug is horibble. Can you fix it as soon as posibble.
thankyou
Such data cannot generate a tree
var dataOne = [
{
id: 100,
name: 'Portfolio',
parent_id: undefined
},
{
id: 200,
name: 'Web Development',
parent_id: 100
},
{
id: 300,
name: 'Recent Works',
parent_id: 200
},
{
id: 400,
name: 'About Me',
parent_id: undefined
}
];
When data had 2 or more top levels 'root' there is stack overflow occured.
Here is representation:
https://repl.it/@Nexis/NotableElegantLaws
Just remove top root level and all works fine.
ArrayToTree returns incorrect array. Last element is function.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.