优化for循环,可以用什么方法

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)

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循环太麻烦了,想简单一点,

这个怎么优化,递归吗,是递归的话怎么写


慕桂英546537
浏览 370回答 1
1回答

冉冉说

&nbsp; function recursive(data){&nbsp; &nbsp; for (var i = 0,l = data.length; i < l; i++) {&nbsp; &nbsp; &nbsp; let item= data[i]&nbsp; &nbsp; &nbsp; if(item.intIsDel === 1){&nbsp; &nbsp; &nbsp; &nbsp; item['disabled'] = true&nbsp; &nbsp; &nbsp; &nbsp; item['strName'] = item['strName'] + ' ' + '(已删除)'&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if(item.listThemeChildren && item.listThemeChildren.length>0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recurFn(item.listThemeChildren)&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp;&nbsp; }let arr =[{&nbsp; intIsDel:1,&nbsp; listThemeChildren:[&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; intIsDel:0&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; intIsDel:1,&nbsp; &nbsp; &nbsp; listThemeChildren:[&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intIsDel:0&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intIsDel:1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ]}];recursive(arr)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript