首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当我使用递归函数时,为什么方括号中的函数参数不起作用

当我使用递归函数时,为什么方括号中的函数参数不起作用
EN

Stack Overflow用户
提问于 2020-04-13 21:14:45
回答 1查看 37关注 0票数 0

我正在使用递归函数,并且我调用了名为"optionVal“的函数参数中的JSON值,以及显示我的函数调用次数的调用计数?现在我面临的问题是,当方括号浏览器中的调用计数显示错误“无法读取未定义的属性'name‘”时,我使用类似于0 1的数字,而不是计算函数正常工作。为什么会发生这种情况,任何人都可以解释。

代码语言:javascript
复制
    var defaultAdd = document.getElementById('dropdown-select');
    var optionVal =
        [
            {
                name: "Animals",
                child: [
                    {
                        name: "Dog",
                        child: [
                            {
                                name: "Huskey"
                            },
                            {
                                name: "German Shephard"
                            }
                        ]
                    },
                    {
                        name: "Fish",
                        child: [
                            {
                                name: "Whale"
                            },
                            {
                                name: "Shark"
                            }
                        ]
                    }
                ]
            },
            {
                name: "Metal",
                child: [
                    {
                        name: "Hard",
                        child: [
                            {
                                name: "Gold"
                            },
                            {
                                name: "Iron"
                            }
                        ]
                    },
                    {
                        name: "Soft",
                        child: [
                            {
                                name: "Alluminium"
                            },
                            {
                                name: "Mercury"
                            }
                        ]
                    }
                ]
            },
            {
                name: "Cloths",
                child: [
                    {
                        name: "Resin",
                    },
                    {
                        name: "Cotton",
                    },
                    {
                        name: "Linen",
                    },
                    {
                        name: "Denim",
                    }
                ]
            },
            {
                name: "Lead",
            }
        ]
    var array = ['<ul>'];

    navbar(optionVal, 0);

    function navbar(optionVal, count) {
        console.log(optionVal[count].name)
        array.push('<li>' + optionVal[count].name + '</li>');

        if (count >= optionVal.length) {
            return false;
        } else {
            navbar(optionVal, count + 1)
        }
    }

    defaultAdd.innerHTML = array.join("");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-13 21:35:52

您的错误是,您超出了数组的限制(第87行"f (count >= optionVal.length - 1)“。参见下面的代码:

代码语言:javascript
复制
var defaultAdd = document.getElementById('dropdown-select');
var optionVal =
    [
        {
            name: "Animals",
            child: [
                {
                    name: "Dog",
                    child: [
                        {
                            name: "Huskey"
                        },
                        {
                            name: "German Shephard"
                        }
                    ]
                },
                {
                    name: "Fish",
                    child: [
                        {
                            name: "Whale"
                        },
                        {
                            name: "Shark"
                        }
                    ]
                }
            ]
        },
        {
            name: "Metal",
            child: [
                {
                    name: "Hard",
                    child: [
                        {
                            name: "Gold"
                        },
                        {
                            name: "Iron"
                        }
                    ]
                },
                {
                    name: "Soft",
                    child: [
                        {
                            name: "Alluminium"
                        },
                        {
                            name: "Mercury"
                        }
                    ]
                }
            ]
        },
        {
            name: "Cloths",
            child: [
                {
                    name: "Resin",
                },
                {
                    name: "Cotton",
                },
                {
                    name: "Linen",
                },
                {
                    name: "Denim",
                }
            ]
        },
        {
            name: "Lead",
        }
    ]
var array = ['<ul>'];

navbar(optionVal, 0);

function navbar(optionVal, count) {
    console.log(optionVal[count].name)
    array.push('<li>' + optionVal[count].name + '</li>');

    if (count >= optionVal.length - 1) { // Your error was here! :)
        return false;
    } else {
        navbar(optionVal, count + 1)
    }

// You can refactor for better readability
// if (count < optionVal.length - 1) { // Max index 4 - 1 = 3
//     navbar(optionVal, count + 1)
// } else {
//     return false;
// }

}

defaultAdd.innerHTML = array.join("");
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {background: white}
    </style>
</head>
<body>
    <ul id="dropdown-select">
        
    </ul>
</body>
<script src="index.js"></script>
</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61188704

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档