简体   繁体   English

Javascript递归:如何对子节点进行父级访问的实例

[英]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 ? 例如,如果关系为: A-> A1-> A11那么我如何访问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. 该建议使用Array#some ,因为如果找到了一个节点,则可以为此级别停止迭代。

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; } 

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

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