[
{id: 0, name: 'Main', path: '/', childs: [1,2]},
{id: 1, name: 'One', path: '/one', childs: [3]},
{id: 2, name: 'Two', path: '/two', childs: []},
{id: 3, name: 'Three', path: '/one/three', childs: []}
]
{
name: 'Main',
path: '/',
children: [
{
name: 'One',
path: '/sub1',
children': [
{
name: 'Three',
path: '/one/three',
children: []
}
]
},
{
name: 'Two',
path: '/two',
children: []
}
]
}
id
находить весь элемент, данные в начале преобразовываются в «словарь» – объект, где свойства это id, а значения элементы.var data = [
{id: 0, name: 'Main', path: '/', childs: [1,2]},
{id: 1, name: 'One', path: '/one', childs: [3]},
{id: 2, name: 'Two', path: '/two', childs: []},
{id: 3, name: 'Three', path: '/one/three', childs: []}
];
function makeTree(d) {
var id, el, i, dict = {}, minId, r;
function parseChildren(el) {
var i, newEl;
for( i = 0; i < el.childs.length; i++) {
newEl = makeElement(dict[ el.childs[i] ]);
parseChildren( newEl);
el.children.push( newEl);
}
delete el.childs;
}
for( i = 0; i < d.length; i++) {
el = d[i];
id = el.id;
if( typeof minId === 'undefined' || minId > id) minId = id;
dict[id] = el;
}
r = makeElement( dict[minId] ); // root element
parseChildren(r);
return r;
}
function makeElement(arrElement) {
return {
name: arrElement.name,
path: arrElement.path,
childs: arrElement.childs,
children: []
};
}
var tree = makeTree(data);
/* {
"name": "Main",
"path": "/",
"children": [{
"name": "One",
"path": "/one",
"children": [{
"name": "Three",
"path": "/one/three",
"children": []
}]
}, {
"name": "Two",
"path": "/two",
"children": []
}]
} */