当第n个嵌套数组没有更多嵌套数组时,递归函数停止

时间:2013-01-17 14:28:16

标签: javascript jquery ajax json

我有JSON文档,它有一些嵌套数组。如果数组有嵌套数组,我希望每次都调用function AAA()。然后在没有更多嵌套数组时停止。

例如。如果我有:

         array[AAA,BBB,CCC]
                |
               AAA[AAA,BBB,CCC]
                    |
                   AAA[AAA,BBB,CCC]
                        |
                       etc.

我想在function AAA()有子阵列时始终致电AAA。假设AAA有5次subAAA。我希望function(AAA)自己调用5次然后停止。如果将来我添加更多子阵列来调用更多次。

如果有帮助的话,我的.json

{
    "navigation" : [

        {
            "name" : "home",
            "href" : "home.html"    
        },

        {
            "name" : "services",
            "href" : "interior.html",
            "navigation" : [
                {
                    "name" : "PROJECT MANAGEMENT",
                    "href" : "interior.html",
                    "navigation" : [
                            {
                                "name" : "PROJECT MANAGEMENT",
                                "href" : "interior.html"
                            },
                            {
                                "name" : "BUSINESS ANALYST",
                                "href" : "interior.html"
                            }
                        ]
                },
                {
                    "name" : "BUSINESS ANALYST",
                    "href" : "interior.html"
                }
            ]
        },

        {
            "name" : "company",
            "href" : "home.html"    
        }


    ]
}

js代码是:

function parseJSON(){

        var navigation = new_json['navigation'];
        var nav_html = '';

        for (var i = 0; i < navigation.length; i++) {

            var name = navigation[i]['name'];
            var href = navigation[i]['href'];
            var submenu = navigation[i]['navigation'];

            nav_html += '<li><a href="' + href + '">' + name + '<span class="ddArrow"></span></a>';     

            if( typeof(submenu) != 'undefined' ){
                nav_html += '<ul>';

                for( var j=0; j<submenu.length; j++ ){

                    var submenu_name = submenu[j]['name'];
                    var submenu_href = submenu[j]['href'];

                    nav_html += '<li><a href="' + submenu_href + '">' + submenu_name + '</a></li>';         
                }
                nav_html += '</ul>';
            }
            nav_html += '</li>';
            console.log( nav_html );
            $('#navigation ul').html( nav_html );
        };  
    };

这样我想创建具有子菜单的导航。现在我有3个级别,但如果我决定添加第4和第5级。我希望我的代码在HTML中解析它们而无需编写更多代码。剩下的我......当函数停止时,我只需要IF

我根本不写我尝试过的东西,因为我明白为什么不起作用。

2 个答案:

答案 0 :(得分:1)

如果我认为只有在包含导航属性时才需要再次运行,那么你可以使用:

// Without using Array.forEach. Array.isArray polyfill may be 
// required to support older browsers.
function getLinks(obj) {
    var i, len;
    if (Array.isArray(obj.navigation)) {
        len = obj.navigation.length;
        for (i = 0; i < len; i += 1) {
            getLinks(obj.navigation[i]);
        }
    }
}

// Using Array.forEach - Polyfill may be required using the 
// forEach method to support older browsers.
function getLinks(obj) {
    if (Array.isArray(obj.navigation)) {
        obj.navigation.forEach(function (link) {
            getLinks(link);
        });
    }
}

// Array.isArray polyfill.
if(!Array.isArray) {
    Array.isArray = function (vArg) {
        return Object.prototype.toString.call(vArg) === "[object Array]";
    };
}

// Array.forEach polyfill.
if (!Array.prototype.forEach) {
    Array.prototype.forEach = function(fn, scope) {
        for(var i = 0, len = this.length; i < len; ++i) {
            fn.call(scope, this[i], i, this);
        }
    }
}

isArray polyfill
forEach polyfill

答案 1 :(得分:1)

递归函数的构建非常简单,与您已有的代码非常相似:

function parseJSON() {
    function makeList(navigation) {
        var nav_html = '';
        for (var i = 0; i < navigation.length; i++) {
            var name = navigation[i]['name'],
                href = navigation[i]['href'],
                submenu = navigation[i]['navigation'];

            nav_html += '<li><a href="' + href + '">' + name + '<span class="ddArrow"></span></a>';     

            if( typeof(submenu) != 'undefined' ){
                nav_html += '<ul>';
                // now here, do not iterate it again!
                // call the function recursively!
                nav_html += makeList(submenu);
                nav_html += '</ul>';
            }
            nav_html += '</li>';
        }
        return nav_html;
    }
    $('#navigation ul').html( makeList( new_json['navigation'] ) );
}