How to compare parent and child records in a nested array?

In the hierarchical structure, I have nodes as shown below:

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

Now I want to check if the connections between the parent and child nodes are defined.

Connections between parent and child are defined as below, for example, between Node -1 and Node -1-1:

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

If there is at least one connection ( 1 entry in the connection properties connection ) between the parent and child nodes, then this is OK, I want to show a warning to the user and would like to return from the iteration function immediately if there is no connection between the nodes.

, , (.. ), id, .

Node -1-1 Node -1-1-1 , , .

, , .

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()">
Hide result
+4
3

, - node . , , , . node. - :

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()">
Hide result
+1

. , , .

: , , , .

node, / . / .

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

check .

connections.joins.

undefined, , .

.

( !o.isParent ..., parent, parent undefined, .)

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" }
Run codeHide result

A slightly better data structure will help if it connections.joinsis just an object, not an array that needs to be iterated to check if this parent / child matches.

+1
source

Source: https://habr.com/ru/post/1681199/


All Articles