The problem
let deps = [
{id: 1, name: 'dep1', pid: 0},
{id: 2, name: 'dep2', pid: 1},
{id: 3, name: 'dep3', pid: 1},
{id: 4, name: 'dep4', pid: 3},
{id: 5, name: 'dep5', pid: 4},
];
let res = formatDeps(deps);
and the res should be:
[
{
"id": 1,
"name": "dep1",
"pid": 0,
"children": [
{
"id": 2,
"name": "dep2",
"pid": 1,
"children": []
},
{
"id": 3,
"name": "dep3",
"pid": 1,
"children": [
// etc,,,
]
}
]
}
]
Top comments (3)
And for now, I find a slim code to resolve it?
Have you tried
reduce
?yep, but to code?