1

I'm trying to run recursive-ly in an array in a react aplication, I got it this way and I want to improve my function to run the array:

my Array:

tree: [
         {
           key: 0 ,
           name: 'menu 1',
           children: [
             {
               key: 1,
               name: 'submenu 1 - 1'
             },{
               key: 2,
               name: 'submenu 1 - 2'
             }
           ]
         },{
           key: 3 ,
           name: 'menu 2',
           children: [
             {
               key: 4,
               name: 'submenu 2 - 1'
             },{
               key: 5,
               name: 'submenu 2 - 2'
             }
           ]
         }
      ]

And I run this Array this way:

const findInArrayTree = (tree, search) =>{
  let it,
      result

  for (it = 0; it < tree.length; it += 1) {
    result = findNode(tree[it], search);
    if(result !== false){
      return result;
    }
  }
  return result;
};

const findNode = (currentNode, search) => {
    let i,
        currentChild,
        result; 
    if (search == currentNode.key) {
        return currentNode;
    } else {
        // Use a for loop instead of forEach to avoid nested functions
        // Otherwise "return" will not work properly
        if(currentNode.children){
          for (i = 0; i < currentNode.children.length; i += 1) {
            currentChild = currentNode.children[i];
            // Search in the current child
            result = findNode(currentChild, search);
            // Return the result if the node has been found
            if (result !== false) {
                return result;
            }
          }
        }
        // The node has not been found and we have no more options
        return false;
    }
};

Someone can help-me to improve this? I want to do this run in one function only, but my brain is broken alredy..

5
  • What is currentNode.key ? Commented Oct 23, 2017 at 13:53
  • sorry I already fix the array.. Commented Oct 23, 2017 at 13:54
  • And what the value of findInArrayTree should be if, for instance, key = 3 ? Commented Oct 23, 2017 at 13:55
  • if key = 3 return: { key: 3 , name: 'menu 2', children: [ { key: 4, name: 'submenu 2 - 1' },{ key: 5, name: 'submenu 2 - 2' } ] } Commented Oct 23, 2017 at 14:15
  • Ok, take a look at my answer, it solves your problem Commented Oct 23, 2017 at 14:17

1 Answer 1

1

You can do it like below:

var tree = [
         {
           key: 0 ,
           name: 'menu 1',
           children: [
             {
               key: 1,
               name: 'submenu 1 - 1'
             },{
               key: 2,
               name: 'submenu 1 - 2'
             }
           ]
         },{
           key: 3 ,
           name: 'menu 2',
           children: [
             {
               key: 4,
               name: 'submenu 2 - 1'
             },{
               key: 5,
               name: 'submenu 2 - 2'
             }
           ]
         }
      ];

function findInTree(items, search) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.key === search) {
            return item;
        } else if (item.children && item.children.length > 0) {
            var foundInChildren = findInTree(item.children, search);
            if (foundInChildren) {
                return foundInChildren;
            }
        }
    }
}

var found = findInTree(tree, 4);
console.log(found);

Sign up to request clarification or add additional context in comments.

1 Comment

Your sugestion works perfectly... thanks a lot friend!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.