如何比较嵌套数组中的父记录和子记录? [英] How to compare parent and child record in nested array?

查看:75
本文介绍了如何比较嵌套数组中的父记录和子记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having a nodes like below in hierarchical structure :

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

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

Now I want to check whether connections is define or not between parent and child nodes.

父节点和子节点之间的连接定义如下,例如在节点1和节点1-1之间:

Connections between parent and child is define like below for eg between Node-1 and Node-1-1 :

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

如果在父节点和子节点之间至少存在1个连接(连接的连接属性中的1条记录),那么就可以了,我想向用户显示警告想要在节点之间没有连接时立即从迭代函数返回。

If there exist at least 1 connection (1 record in joins property of connections) between parent and child nodes then it's fine else I want to show alert to user and would like to return from the iterate function immediately on encounter of no connection between nodes.

所以除非我从迭代函数得到响应(即我terate函数没有完成)我不想增加我的id,这就是为什么我将回调函数传递给迭代函数并希望返回响应。

So unless and until I get a response from iterate function(i.e iterate function is not completed) I don't want to increment my id that is why I am passing callback to iterate function and would like to return response.

因为有Node-1-1和Node-1-1-1之间没有连接,所以我想向用户显示警告,因为连接的连接属性中没有记录。

As there is no connections between Node-1-1 and Node-1-1-1 so I would like to show alert to user because there is no record in joins property of connections.

但问题是我没有得到如何比较每个父节点和子节点以及如何在递归结构中管理这个回调。

But problem is I am not getting how to compare every parent and child nodes and how to manage this callback in recursive structure.

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()">

推荐答案

你可以做一个递归函数来做这个,函数有两个参数 - 节点及其父节点。在每次递归时检查是否存在可用的连接,并且连接中的父级与递归期间传递的父级相同。连接中的子节点应该是当前节点名称。这样的事情:

you can make a recursive function to do this, function takes two arguments-- node and its parent. At each recursion check if there are joins available and parent in joins is same as parent that was passed during recursion. And child in joins should be current node name. Something like this:

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()">

这篇关于如何比较嵌套数组中的父记录和子记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆