How to combine dimensional arrays

var list1 = [ { id: 'node1', children: [ { id: 'node11', children: [] } ] } ]; var list2 = [ { id: 'node1', children: [ { id: 'node13', children: [] } ] } ]; var resultList = [ { id: 'node1', children: [ { id: 'node11', children: [] }, { id: 'node13', children: [] } ] } ]; 

All my arrays are trees, one node can belong to only one parent. I want to combine list1 with list2 and get resultList.I tried many ways, recursive callback, search and replace strings, etc., but I still could not understand.

+6
source share
2 answers

The following code combines all levels of two arrays of trees, and not just at the highest level:

 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>'); 

Watch the code live on JSBin: http://jsbin.com/wikaricita/3/edit?js,output

Note that this algorithm has O (n ^ 2) complexity, which means that it will not scale very well. If the trees are getting very large or performance is a critical issue, you probably want to explore other ways to solve this problem.

0
source

If I understand you correctly, you want it to be compact with id .

 function getCompactById(arr) { // must have the same id var res = []; var obj = {}; obj.id = arr[0][0].id; obj.children = []; for(var i = 0; i < arr.length; i += 1) { obj.children.push(arr[i][0].children[0]); } res.push(obj); return res; } 

The array looks like var arr = [list1, list2]; In function create an array and object . The object receives an identifier and an array. The identifier is always the same, so we take it from the first array. Go through the array and click on all arr[i][0].children[0] objects. After the loop, press obj in the array. Return an array of results.

Demo

+2
source

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


All Articles