简体   繁体   English

Javascript在递归数组中寻找价值

[英]Javascript finding value in recursive array

I made a function in order to find an element in a tree object. 我做了一个函数,以便在树对象中找到一个元素。 My function works, but sometimes the function don't find the value and stop before looking all the tree. 我的函数可以工作,但是有时函数在查找所有树之前找不到值并停止。

I can't explain why it works sometimes and sometimes not. 我无法解释为什么有时有时不起作用。

Here is my fiddle : http://jsfiddle.net/3cdwA/2/ 这是我的小提琴: http : //jsfiddle.net/3cdwA/2/

When you click on categories like "Sciences" you can see that it works. 当您单击“科学”等类别时,您会看到它起作用。 But if you click on "Bandes-Dessinées" it should display "Comics" but it doesn't. 但是,如果单击“Bandes-Dessinées”,它应该显示“ Comics”,但不会。

Here is my recursive function : 这是我的递归函数:

function getChildrenFromCurrentFolder(tree, targetFolder) {
console.log(tree);
// Find recursivly all direct children of targeted folder
if (targetFolder == tree.id) {
   return tree.folders;
} else if (tree.folders.length > 0) {
   var folders = [];
   for (i = 0; folders.length == 0 && i < tree.folders.length; i++) {
      folders = getChildrenFromCurrentFolder(tree.folders[i], targetFolder);
   }
   return folders;
}
return [];

} }

here is my test tree : 这是我的测试树:

tree = {
   'id': 1,
   'name': 'Mes Bookmarks',
   'folders': [
      {
         'id': 2,
         'name': 'Sciences',
         'folders': [
            {
                'id': 3,
                'name': 'Biologie',
                'folders': [
                   {
                      'id': 12,
                      'name': 'Neurologie',
                      'folders': []
                   }
                ]
            },
            {
                'id': 4,
                'name': 'Astrophysique',
                'folders': [
                   {
                      'id': 8,
                      'name': 'Cosmologie',
                      'folders': [
                         {
                            'id': 10,
                            'name': 'Système solaire',
                            'folders': []
                         }
                      ]
                   },
                   {
                      'id': 9,
                      'name': 'Trous noirs',
                      'folders': []
                   }
                ]
            },
            {
                'id': 5,
                'name': 'Mathématiques',
                'folders': []
            }
         ]
      },
      {
         'id': 6,
         'name': 'Actualités',
         'folders': [
            {
                'id': 11,
                'name': 'Monde',
                'folders': []
            }
         ]
      },
      {
         'id': 7,
         'name': 'Bandes-dessinées',
         'folders': [
            {
                'id': 13,
                'name': 'Comics',
                'folders': []
            }
         ]
      }
   ]
};

It's a simple, but common mistake. 这是一个简单但常见的错误。 You forgot to declare your loop variables and that's trouble when doing recursion as you're creating a global that's being reused: 您忘记了声明循环变量,而在创建要重用的全局变量时进行递归操作很麻烦:

function displayFolders() {
    ...
       for (var i = folders.length-1; i >= 0; i--)
    ...    --^--
}

function getChildrenFromCurrentFolder(tree, targetFolder) {
    ...
       for (var i = 0; folders.length == 0 && i < tree.folders.length; i++) {
    ...    --^--
}

function getBreadcrumb(tree, targetFolder, breadcrumb) {
    ...
    for (var i = 0; i < tree['folders'].length; i++)
    ... --^--
}

I'm not sure all the other logic is correct, but this definitely changes the behavior. 我不确定其他所有逻辑是否正确,但这肯定会改变行为。

http://jsfiddle.net/3cdwA/4/ http://jsfiddle.net/3cdwA/4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM