Il seguente codice unire tutti i livelli dei due array di alberi, non solo sul livello più alto:
var list1 = ...
var list2 = ...
var addNode = function(nodeId, array) {
array.push({id: nodeId, children: []});
};
var placeNodeInTree = function(nodeId, parent, treeList) {
return treeList.some(function(currentNode){
// If currentNode has the same id as the node we want to insert, good! Required for root nodes.
if(currentNode.id === nodeId) {
return true;
}
// Is currentNode the parent of the node we want to insert?
if(currentNode.id === parent) {
// If the element does not exist as child of currentNode, create it
if(!currentNode.children.some(function(currentChild) {
return currentChild.id === nodeId;
})) addNode(nodeId, currentNode.children);
return true;
} else {
// Continue looking further down the tree
return placeNodeInTree(nodeId, parent, currentNode.children);
}
});
};
var mergeInto = function(tree, mergeTarget, parentId) {
parentId = parentId || undefined;
tree.forEach(function(node) {
// If parent has not been found, placeNodeInTree() returns false --> insert as root element
if(!placeNodeInTree(node.id, parentId, mergeTarget)){
list1.push({id: node.id, children:[]});
}
mergeInto(node.children, mergeTarget, node.id);
});
};
mergeInto(list2, list1);
document.write('<pre>');
document.write(JSON.stringify(list1, null, 4));
document.write('</pre>');
vedere il codice in diretta su JSBin: http://jsbin.com/wikaricita/3/edit?js,output
Si noti che questo algoritmo ha una complessità di O (n^2), il che significa che non si ridimensionerà molto bene. Se gli alberi diventano molto grandi o le prestazioni sono un problema critico, probabilmente vorrai esaminare altri modi per risolvere questo problema.
la lista3 è il risultato atteso. – Aflext
I tuoi alberi array? Oppure un elemento può avere più genitori? Se si suppone che sia un albero, come si desidera gestire le definizioni in conflitto tra i due array che si desidera unire? – Timo
Sì, i due array sono alberi. Quello che voglio fare è unire un nuovo array tree dal server a un array tree esistente nella memoria DOM come "aggiorna l'albero visualizzato". – Aflext