递归函数在第n个嵌套数组不再有嵌套数组时停止 [英] Recursive function to stop when n-th nested array has no more nested arrays

查看:81
本文介绍了递归函数在第n个嵌套数组不再有嵌套数组时停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些嵌套数组的JSON文档.而且我想制作function AAA(),它在每次数组嵌套数组时都可以调用.然后在没有更多嵌套数组时停止.

I have JSON document which has some nested arrays. And I want to make function AAA() which recalls every time if an array has nested array. And then stop when there isn't more nested arrays.

例如.如果我有:

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

我想总是在AAA有子数组时调用function AAA().假设AAAsubAAA的5倍.我希望function(AAA)自我调用5次,然后停止.而且,如果将来我添加更多的子数组来调用更多次.

I want to call function AAA() always when AAA has a subarray. Let's say AAA has 5 times subAAA. I want the function(AAA) to call itself 5 times and then stop. And if in future I add more subarrays to call more times.

这对我有帮助吗?

If it will help here is my .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键即可.

That way I want to create navigation which has submenus. Now I have 3 levels, but if I decide to add 4th and 5th. I want my code to parse them in the HTML without writing more code. I have the rest ... I just need to the IF when function stops.

我只是不写自己尝试过的内容,因为我知道为什么不起作用.

I simply don't write what I have tried because I understand why is not working.

推荐答案

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

A recursive function is quite simple to build, and is very similar to the code you already have:

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'] ) );
}

这篇关于递归函数在第n个嵌套数组不再有嵌套数组时停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆