2 回答

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
這需求改的和之前差的很大哦...
這個(gè)答案是應(yīng)之前的需求:返回所有admin,并且刪除children中admin。
getRoles(getData());
function getRoles(data, role = 'admin') {
let resArr = [];
main(data);
return resArr;
function main(data) {
if (data && data.length) {
data.forEach((d, i) => {
if (d.role === 'admin') resArr.push(data.splice(i, 1));
if (d.children && d.children.length) main(d.children);
});
}
}
}
function getData() {
return [{
role: 'other',
children: [{
role: 'admin',
index: '1'
}, {
role: 'other'
}]
},{
role: 'admin',
index: '2',
children: [{
role: 'other',
children: [{
role: 'admin',
index: '3'
}]
}]
}];
}

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊
let arr = [{
role: 'admin',
left: 'fzz',
children: [{
role: 'other',
right: 'pdd',
children: [{
role: 'admin'
}]
}]
}]
arr.find(function(x){
return x.role ==='admin';
})
添加回答
舉報(bào)