简体   繁体   中英

Javascript Recursion: How to the instance of parent access of the child node

i'm trying to build the logic to get access to the all parent nodes of the child I've currently access to via recursion,but not able to do so. here's what I've done so far:

Here's my array:

var results = [{
  "key": 1,
  "name": "A",
  "child": [{
    "key": 2,
    "name": "A1",
    "child": [{
      "key": 1473591350189,
      "name": "A11"
    }]
  }, {
    "key": 10,
    "name": "A2",
    "child": []
  }]
}, {
  "key": 66,
  "name": "B",
  "child": [{
    "key": 67,
    "name": "B1",
    "child": [{
      "key": 68,
      "name": "B11",
      "child": [{
        "key": 69,
        "name": "B111",
        "child": []
      }]
    }]
  }]
}];

Now my logic is for iteration:

function recursionFn(results, parentNode) {
  for (var i = 0; i < results.length; i++) {
    var _node = results[i];
    if (_node.child.length > 0) {
      console.log('Name: ' + _node.name + ' First Childs: ' + _node.child.length);
      if (parentNode)
        console.log('ParentNode: ' + parentNode.name);
    }
    if (_node.child.length > 0)
      recursionFn(_node.child, _node);
    if (_node.child.length == 0) {
      console.log('Name: ' + _node.name + ' Second Childs: ' + _node.child.length);
      if (parentNode)
        console.log('ParentNode: ' + parentNode.name);
    }
  }
}

Here I'm getting the immediate parent of the current child I'm in but how do I get access to all the parents of that child. For instance, if the relationship is: A -> A1 -> A11 Then how do I get access to all parents of A11 ? Thanks in advance!!

Walking up the parents is better done in a loop rather than recursion. Walking all nodes in a hierarchy is (arguably) better done with recursion than in a loop.

I suggest this to walk parents

function navParents(node)
{
    if(!node)
       return;
    while(node != null) {
       /* do something with node here */
       node = node.parent;
    }
}

You could use an iterative recursive approach for seaching the parent nodes of a wanted node, which is defined in a callback.

This proposal uses Array#some , because if a node is found, then the iteration can be stopped for this level.

The result array contains all parent nodes from the root to the final node.

 function getParentNodes(tree, callback) { var nodes = []; tree.some(function iter(a) { if (callback(a) || Array.isArray(a.child) && a.child.some(iter)) { nodes.unshift(a); return true; } }); return nodes; } var results = [{ key: 1, name: "A", child: [{ key: 2, name: "A1", child: [{ key: 1473591350189, name: "A11" }] }, { key: 10, name: "A2", child: [] }] }, { key: 66, name: "B", child: [{ key: 67, name: "B1", child: [{ key: 68, name: "B11", child: [{ key: 69, name: "B111", child: [] }] }] }] }]; console.log(getParentNodes(results, function (o) { return o.name === 'A11'; })); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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