代码之家  ›  专栏  ›  技术社区  ›  Learning-Overthinker-Confused

如何比较嵌套数组中的父记录和子记录?

  •  4
  • Learning-Overthinker-Confused  · 技术社区  · 7 年前

    我在层次结构中有如下节点:

    Node - 1
         Node-1-1
           Node-1-1-1
    

    现在我想检查是否定义了父节点和子节点之间的连接。

    "connections": {
              "joins": [
                {
                  "parent": "Node-1",
                  "child": "Node-1-1"
                }
              ]
            }
    

    如果至少存在一个连接( 1连接的连接属性中的记录 )在父节点和子节点之间,然后我想向用户显示警报,并希望在遇到节点之间没有连接时立即从迭代函数返回。

    所以,除非我从迭代函数得到响应(即迭代函数未完成),否则我不想增加我的id,这就是我向迭代函数传递回调并希望返回响应的原因。

    var records = [
      {
        "name": "Node-1",
        "nodes": [
          {
            "name": "Node-1-1",
            "isParent": false,
            "nodes": [
              {
                "name": "Node-1-1-1",
                "isParent": false,
                "nodes": [
                  
                ],
                "connections": {
                  "joins": []
                }
              }
            ],
            "connections": {
              "joins": [
                {
                  "parent": "Node-1",
                  "child": "Node-1-1"
                }
              ]
            }
          }
        ],
        "isParent": true
      }
    ];
    
    
    function CheckConnections(){
         var id=0;
         iterate(records,
                    function (valid) {
                       if(valid)
                       {
                          id = id + 1;
                          console.log(id);
                       }
                      else
                          alert("please define connections")
                    }
                ); 
         
    }
    
    function iterate(nodes,callback)
    {
       var connectionDefine = false;
       
       callback(false);
    }
    <input type="button"  value="Check Connections"  onclick="CheckConnections()">
    3 回复  |  直到 7 年前
        1
  •  2
  •   Fabien    7 年前

    下面的递归解决方案将向您显示遇到的第一个缺少连接关系的错误。我添加了评论,以便您可以跟踪发生了什么。

    var records = [{"name":"Node-1","nodes":[{"name":"Node-1-1","isParent":false,"nodes":[{"name":"Node-1-1-1","isParent":false,"nodes":[],"connections":{"joins":[]}}], "connections":{"joins":[{"parent":"Node-1","child":"Node-1-1"}]}}],"isParent":true}];
    
    function connections_control(records, parent = undefined) {
        // Browse the nodes list
        for (var node of records) {
            // Control if the keys we need do exist
            if (parent && node.connections && node.connections.joins) {
                var found = false;
                // Search in connections the current relation parent/child
                for (var connection of node.connections.joins) {
                    if (connection.parent == parent && connection.child == node.name) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    // We checked all connections, but we did not find our current relation!
                    console.log('Warning: Broken connection between parent node '+parent+' and child node '+node.name);
                    break;
                }
            }
            if (node.nodes) {
                // The current node becomes parent, start again with the inner nodes list
                connections_control(node.nodes, node.name);
            }
        }
    }
    
    connections_control(records);
    

    注意,在第一个循环中,将文档置于其根目录下,没有父循环,因此没有搜索连接。

    nodejs childs.js
    Warning: Broken connection between parent node Node-1-1 and child node Node-1-1-1
    
        2
  •  1
  •   Dij    7 年前

    可以使用递归函数来实现这一点,该函数有两个参数——节点和其父节点。在每次递归时,检查是否有可用的联接,并且联接中的父级与递归期间传递的父级相同。连接中的子节点应为当前节点名。类似这样:

    var records = [
      {
        "name": "Node-1",
        "nodes": [
          {
            "name": "Node-1-1",
            "isParent": false,
            "nodes": [
              {
                "name": "Node-1-1-1",
                "isParent": false,
                "nodes": [
                  
                ],
                "connections": {
                  "joins": []
                }
              }
            ],
            "connections": {
              "joins": [
                {
                  "parent": "Node-1",
                  "child": "Node-1-1"
                }
              ]
            }
          }
        ],
        "isParent": true
      }
    ];
    
    
    function CheckConnections(){
         var id=0;
         var inValidNodes = [];
         records.forEach(function(node){
           inValidNodes = checkValidConnections(node, null);
           if (inValidNodes.length > 0)
               return;
         });
    
         if(inValidNodes.length === 0)
         {
              id = id + 1;
              console.log(id);
          } else {
             alert("please define connections " + inValidNodes);
          }
    }
    function checkValidConnections(node, parent){
           var nodeName = node.name;
           if(!node.isParent){
              var currentParentCondition = node.connections.joins.length > 0 &&
                                       node.connections.joins[0].parent === parent &&
                                       node.connections.joins[0].child === nodeName;
              if (!currentParentCondition)
                  return [parent, nodeName];
           }
    
           if (node.nodes.length > 0){
              return checkValidConnections(node.nodes[0], nodeName);
           } else{
              return [];
           }           
    }
    <input type="button"  value="Check Connections"  onclick="CheckConnections()">
        3
  •  1
  •   Nina Scholz    7 年前

    可以使用递归方法调用 check

    connections.joins 已测试。

    undefined ,这意味着所有连接都已定义,或者您将获得第一个缺少连接的对象。

    (用这个,一个像 !o.isParent ... parent 可以改为使用,因为在第一次调用中 未定义,这将阻止检查。)

    function check(array, parent) {
        var missing;
        array.some(function (o) {
            var j = o.connections && o.connections.joins && o.connections.joins[0];
            if (!o.isParent && (!j || j.parent !== parent || j.child !== o.name)) {
                return missing = { parent: parent, child: o.name };
            }
            if (o.nodes) {
                return missing = check(o.nodes, o.name);
            }                
        });
        return missing;
    }
    
    var records0 = [{ name: "Node-1", nodes: [{ name: "Node-1-1", isParent: false, nodes: [{ name: "Node-1-1-1", isParent: false, nodes: [], connections: { joins: [{ parent: "Node-1-1", child: "Node-1-1-1" }] } }], connections: { joins: [{ parent: "Node-1", child: "Node-1-1" }] } }], isParent: true }],
        records1 = [{ name: "Node-1", nodes: [{ name: "Node-1-1", isParent: false, nodes: [{ name: "Node-1-1-1", isParent: false, nodes: [], connections: { joins: [ /* missing */ ] } }], connections: { joins: [{ parent: "Node-1", child: "Node-1-1" }] } }], isParent: true }];
    
    console.log(check(records0)); // undefined, nothing missing
    console.log(check(records1)); // missing { parent: "Node-1-1", child: "Node-1-1-1" }

    如果可以,稍微好一点的数据结构会有所帮助 连接.joins 只是一个对象而不是数组,需要迭代来检查给定的父/子匹配。