JS递归

Posted BadGirl_Xiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS递归相关的知识,希望对你有一定的参考价值。

普通写法:

技术分享
   var twoLevel = data[‘result‘];
                            //console.log(twoLevel.length);   4
                            var html = "";
                            for (var i = 0; i < twoLevel.length; i++) {
                                
                                var threeLevel = twoLevel[i].children;
                                if (threeLevel.length > 0) {
                                    html += "<li><a class=‘inactive‘>" + twoLevel[i].displayName + "</a>";
                                    html += "<ul style=‘display: none‘>";
                                    for (var j = 0; j < threeLevel.length; j++) {
                                        var fourLevel = threeLevel[j].children;
                                        if (fourLevel.length > 0) {
                                            html += "<li><a class=‘inactive‘>" + threeLevel[j].displayName + "</a>";
                                            html += "<ul style=‘display: none‘>";
                                            for (var k = 0; k < fourLevel.length; k++) {


                                                var fiveLevel = fourLevel[k].children;
                                                if (fiveLevel.length > 0) {
                                                    html += "<li><a class=‘inactive‘>" + fourLevel[k].displayName + "</a>";
                                                    html += "<ul style=‘display: none‘>";
                                                    for (var w = 0; w < fourLevel.length; w++) {
                                                        html += "<li><a target=‘iframe‘ href=" + fiveLevel[w].url + ">" + fiveLevel[w].displayName + "</a>";
                                                    }
                                                    html += "</ul>";
                                                } else {
                                                    html += "<li><a  target=‘iframe‘ href=‘" + fourLevel[k].url + "‘>" + fourLevel[k].displayName + "</a>";
                                                }

                                                html += "</li>";

                                              
                                            }
                                            html += "</ul>";
                                        } else {
                                            html += "<li><a  target=‘iframe‘ href=‘" + threeLevel[j].url + "‘>" + threeLevel[j].displayName + "</a>";
                                        }
                                        
                                        html += "</li>";

                                    }
                                    html += "</ul>";
                                }
                                else {
                                    html += "<li><a  target=‘iframe‘ href=‘" + twoLevel[i].url + "‘>" + twoLevel[i].displayName + "</a>";
                                }
                                html+="</li>";
                            }
View Code

递归写法:

技术分享
var twoLevel = data[‘result‘];
                            //console.log(twoLevel.length);   4
                            var html = "";
                            function test(childMenu) {
                                for (var i = 0; i < childMenu.length; i++) {

                                    var childData = childMenu[i].children;
                                    if (childData.length > 0) {
                                        
                                        html += "<li><a class=‘inactive‘>" + childMenu[i].displayName + "</a>";
                                        html += "<ul style=‘display: none‘>";
                                        test(childData);
                                        html += "</ul>";

                                    }
                                    else {
                                        html += "<li><a  target=‘iframe‘ href=‘" + childMenu[i].url + "‘>" + childMenu[i].displayName + "</a>";
                                    }
                                    
                                    html += "</li>";
                                }
                            }
                            test(twoLevel);
View Code

 

以上是关于JS递归的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

js代码片段: utils/lcoalStorage/cookie

CSP核心代码片段记录

JS代码片段:一个日期离现在多久了

js常用代码片段(更新中)