在JS中递归搜索嵌套数组

时间:2019-01-11 11:45:26

标签: javascript

我正在尝试在嵌套的json数组中搜索具有匹配名称的单个对象。我的数据结构如下:

[
    {
        "type": "directory",
        "name": "/home/user/test-tree",
        "contents": [
            {
                "type": "directory",
                "name": "my-folder",
                "contents": [
                    {
                        "type": "directory",
                        "name": "nested-folder",
                        "contents": []
                    }
                ]
            },
            {
                "type": "directory",
                "name": "node_modules",
                "contents": [
                    {
                        "type": "directory",
                        "name": "gunzip-file",
                        "contents": []
                    }
                ]
            }
        ]
    }
]

因此,在此示例中,我可能正在搜索一个名为“ node_modules”的目录,它应返回整个对象,包括其内容。这只是一个示例,我的实际数据集可能非常大-例如,树可以表示文件系统上的所有目录。

这是我现在正在使用的代码-似乎可以在此示例中使用,但似乎无法在较大的数据集上正常工作,并且我真的看不到它有什么问题,所以如果有人可以发现任何我会感激的东西。

    function treeSearch(array, dirName) {
        for (let i = 0; i < array.length; i++) {
            if (array[i].name === dirName) {
                return array[i]
            }
            else if (array[i].contents) {
                if (array[i].contents.length > 0) {
                    return treeSearch(array[i].contents, dirName)
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

如果找到该值,则可以采用递归函数对数组进行迭代,并可能造成短循环。

function find(array, value) {
    var result;
    array.some(o => result = o.name === value && o || find(o.contents, value));
    return result || undefined;
}

var data = [{ type: "directory", name: "/home/user/test-tree", contents: [{ type: "directory", name: "my-folder", contents: [{ type: "directory", name: "nested-folder", contents: [] }] }, { type: "directory", name: "node_modules", contents: [{ type: "directory", name: "gunzip-file", contents: [] }] }] }]

console.log(find(data, "node_modules"));
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

您的代码中有错误,目前,您仅在三个分支中的一个分支上递归,就没有在整个数据结构上递归,实际上,如果您要搜索“ node_modules”代码返回未定义。

尝试将这种微小的修改应用于您的代码

const data = [
    {
        "type": "directory",
        "name": "/home/user/test-tree",
        "contents": [
            {
                "type": "directory",
                "name": "my-folder",
                "contents": [
                    {
                        "type": "directory",
                        "name": "nested-folder",
                        "contents": []
                    }
                ]
            },
            {
                "type": "directory",
                "name": "node_modules",
                "contents": [
                    {
                        "type": "directory",
                        "name": "gunzip-file",
                        "contents": []
                    }
                ]
            }
        ]
    }
];

function treeSearch(array, dirName) {
  for (let i = 0; i < array.length; i++) {
     if (array[i].name === dirName) {
       return array[i];
     }
     else if (array[i].contents && array[i].contents.length) {
       const result = treeSearch(array[i].contents, dirName);
       // return the result only if it's actually found otherwise keep looping
       if(result) return result;
     }
   }
}

console.log(treeSearch(data, "node_modules"));