js递归函数
function recursion(data, current) {
var result = null;
if (!data) {
return;
}
for (var i = 0; i < data.length; i++) {
var item = data[i];
if (item.id === current) {
result = item.name;
return result;
}
if (item.children && item.children.length > 0) {
result = this.recursion(item.children, current);
if (result) return result;
}
}
}
数据格式
let items=[{
id: 1,
name: 'Vuetify Human Resources',
children: [{
id: 2,
name: 'Core team',
children: [{
id: 201,
name: 'John'
},
{
id: 202,
name: 'Kael'
},
{
id: 203,
name: 'Nekosaur'
},
{
id: 204,
name: 'Jacek'
},
{
id: 205,
name: 'Andrew'
}
]
},
{
id: 3,
name: 'Administrators',
children: [{
id: 301,
name: 'Ranee'
},
{
id: 302,
name: 'Rachel'
}
]
},
{
id: 4,
name: 'Contributors',
children: [{
id: 401,
name: 'Phlow'
},
{
id: 402,
name: 'Brandon'
},
{
id: 403,
name: 'Sean'
}
]
}
]
},
{
id: 5,
name: 'Example',
children: [{
id: 501,
name: 'qaz'
}]
},
{
id: 6,
name: 'Qdd',
children: [{
id: 601,
name: 'yui'
}]
}
]