相关代码// 请把代码文本粘贴到下方(请勿用图片代替代码)for (var i = 0,l = res.length; i < l; i++) { if (res[i].intIsDel === 1) { res[i]['disabled'] = true res[i]['strName'] = res[i]['strName'] + ' ' + '(已删除)' } for (var j = 0; j < res[i].listThemeChildren.length; j++) { if (res[i].listThemeChildren[j].intIsDel === 1) { res[i].listThemeChildren[j]['disabled'] = true res[i].listThemeChildren[j]['strName'] = res[i].listThemeChildren[j]['strName'] + ' ' + '(已删除)' } for (var r = 0; r < res[i].listThemeChildren.length; r++) { if (res[i].listThemeChildren[j].listThemeChildren[r].intIsDel === 1) { res[i].listThemeChildren[j].listThemeChildren[r]['disabled'] = true res[i].listThemeChildren[j].listThemeChildren[r]['strName'] = res[i].listThemeChildren[j].listThemeChildren[r]['strName'] + ' ' + '(已删除)' } } }}问题描述感觉for循环太麻烦了,想简单一点,这个怎么优化,递归吗,是递归的话怎么写
1 回答
冉冉说
TA贡献1877条经验 获得超1个赞
function recursive(data){
for (var i = 0,l = data.length; i < l; i++) {
let item= data[i]
if(item.intIsDel === 1){
item['disabled'] = true
item['strName'] = item['strName'] + ' ' + '(已删除)'
}
if(item.listThemeChildren && item.listThemeChildren.length>0){
recurFn(item.listThemeChildren)
}
}
}
let arr =[{
intIsDel:1,
listThemeChildren:[
{
intIsDel:0
},
{
intIsDel:1,
listThemeChildren:[
{
intIsDel:0
},
{
intIsDel:1
}
]
}
]
}];
recursive(arr)
添加回答
举报
0/150
提交
取消