数组和子数组上的 Javascript 递归

标签 javascript arrays recursion

有一个高级时刻,并且努力让递归方法在 Javascript 中正确工作。

这里有类似的问答,但到目前为止我没有看到对我有帮助的东西。

也就是说,如果确实有重复,我会删除这个问题。

给定以下对象数组:

var collection = [
                    {
                        id: 1,
                        name: "Parent 1",
                        children: [
                            { id: 11, name: "Child 1", children: [] },
                            { id: 12, name: "Child 2", children: [] }
                        ]
                    },
                    {
                        id: 2,
                        name: "Parent 2",
                        children: [
                            {
                                id: 20,
                                name: "Child 1",
                                children: [
                                    { id: 21, name: "Grand Child 1", children: [] },
                                    { id: 22, name: "Grand Child 2", children: [] }
                                ]
                            }
                        ]
                    },
                    {
                        id: 3,
                        name: "Parent 3",
                        children: [
                            { id: 31, name: "Child 1", children: [] },
                            { id: 32, name: "Child 2", children: [] }
                        ]
                    },
                ];

我已经尝试了几次,但我的方法似乎只通过一个级别后就提前返回了。

我最近的尝试是:

谁能给我指出正确的方向。

function findType(col, id) {


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

                        if (col[i].id == id) {
                            return col[i];

                        }

                        if (col[i].children.length > 0) {
                           return findType(col[i].children, id);

                        }
                    }

                    return null;

                }

我正在尝试查找给定 id 所在的对象匹配,所以寻找 id 1应该返回名称为 Parent 1 的整个对象.如果寻找 id 31然后是 ID 为 31 的整个对象和名字Child 1应该归还。

这将转化为

var t = findType(collection, 1);

var t = findType(collection, 31);

注意我希望获得有关纯 JavaScript 解决方案的帮助,而不是插件或其他库。尽管它们可能更稳定,但这对学习曲线没有帮助。谢谢。

最佳答案

你很接近,你需要一个变量来存储 find 的嵌套调用的临时结果,如果找到,则通过返回找到的对象来打破循环。

没有,如果第一次没有找到,您将返回任何找到的 child ,而不会迭代到数组的末尾。

function findType(col, id) {
    var i, temp;
    for (i = 0; i < col.length; i++) {
        if (col[i].id == id) {
            return col[i];
        }
        if (col[i].children.length > 0) {
            temp = findType(col[i].children, id); // store result
            if (temp) {                           // check
                return temp;                      // return result
            }
        }
    }
    return null;
}

var collection = [{ id: 1, name: "Parent 1", children: [{ id: 11, name: "Child 1", children: [] }, { id: 12, name: "Child 2", children: [] }] }, { id: 2, name: "Parent 2", children: [{ id: 20, name: "Child 1", children: [{ id: 21, name: "Grand Child 1", children: [] }, { id: 22, name: "Grand Child 2", children: [] }] }] }, { id: 3, name: "Parent 3", children: [{ id: 31, name: "Child 1", children: [] }, { id: 32, name: "Child 2", children: [] }] }];

console.log(findType(collection, 31));
console.log(findType(collection, 1));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于数组和子数组上的 Javascript 递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706798/

相关文章:

java - String[] 取空值

java - 为什么 JPA OneToMany 映射 findAll 方法返回递归对象?

C 程序 : Print Linked List from Recursive Ordering Function

java - 展开递归对不同类型变量的影响

"class"内的 JavaScript Image 对象

javascript - 文本区域中的标签

javascript - 我从我网站上的评论表单中收到空电子邮件

java - 交换数组中对应的元素

javascript - javascript 字典的键不存储为值,而是存储为变量名

python - 根据 numpy 数组的值更改其各个元素