![](/img/trans.png)
[英]How to convert a nested array with parent child relationship to a plain array?
[英]How to compare parent and child record in nested array?
我在層次結構中有如下節點:
Node - 1
Node-1-1
Node-1-1-1
現在我想檢查父節點和子節點之間是否定義了連接。
父節點和子節點之間的連接定義如下,例如節點1和節點1-1之間:
"connections": {
"joins": [
{
"parent": "Node-1",
"child": "Node-1-1"
}
]
}
如果在父節點和子節點之間至少存在1個連接( 連接的連接屬性中有1個記錄 ),那么就可以了,我想向用戶顯示警告,並希望在遇到節點之間沒有連接時立即從迭代函數返回。
所以除非並且直到我從迭代函數得到響應(即迭代函數未完成),我不想增加我的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()">
以下遞歸解決方案將顯示遇到的第一個缺少的連接關系時的錯誤。 我添加了評論,以便您可以跟蹤發生的情況。
它不會檢查所有連接:當它找到一個在child和parent之間正確建立的連接時,它會快速跳轉到下一個節點。
基本上,它包括為每個子節點搜索是否正確記錄了當前父/子關系的連接。 它應該適用於任意數量的子節點/嵌套級別。
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
你可以創建一個遞歸函數來執行此操作,函數需要兩個參數 - 節點及其父節點。 在每次遞歸時檢查是否存在可用的連接,並且連接中的父級與遞歸期間傳遞的父級相同。 連接中的子節點應該是當前節點名稱。 像這樣的東西:
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()">
你可以使用一個遞歸方法調用check
與陣列和父名。
在里面,測試了connections.joins
的第一個對象。
結果是undefined
,這意味着定義了所有連接,或者您獲得了第一個缺少連接的對象。
檢查從調用對象獲取父名稱。
(有了這個,可以省略像!o.isParent ...
的檢查,只需要檢查parent
,因為在第一次調用中, 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" }
如果connections.joins
只是一個對象而不是一個數組,那么稍微好一點的數據結構會有所幫助,這需要迭代來檢查給定的父/子是否匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.