Last active
September 23, 2022 18:50
-
-
Save mrmuminov/309f5a25f39097241c20d22a1295913b to your computer and use it in GitHub Desktop.
Find children parents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const datas = { | |
'children': [ | |
{ | |
'name': '1', | |
'children': [ | |
{'name': '1.1'}, | |
{'name': '1.2'}, | |
{ | |
'name': '1.3', | |
'children': [ | |
{'name': '1.3.1'}, | |
{'name': '1.3.2'} | |
] | |
}, | |
{'name': '1.4'} | |
] | |
}, | |
{ | |
'name': '2', | |
'children': [ | |
{'name': '2.1'} | |
] | |
} | |
] | |
}; | |
function findParents(node, searchForName) { | |
if(node.name === searchForName) { | |
return [] | |
} | |
if(Array.isArray(node.children)) { | |
for(const treeNode of node.children) { | |
const childResult = findParents(treeNode, searchForName) | |
if(Array.isArray(childResult)) { | |
return [ treeNode.name ].concat( childResult ); | |
} | |
} | |
} | |
} | |
console.log(findParents(datas, '1.3.2')); | |
/* | |
[ | |
"1", | |
"1.3", | |
"1.3.2" | |
] | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$datas = [ | |
'children' => [ | |
[ | |
'name' => '1', | |
'children' => [ | |
['name' => '1.1'], | |
['name' => '1.2'], | |
[ | |
'name' => '1.3', | |
'children' => [ | |
['name' => '1.3.1'], | |
['name' => '1.3.2'], | |
], | |
], | |
['name' => '1.4'], | |
], | |
], | |
[ | |
'name' => '2', | |
'children' => [ | |
['name' => '2.1'], | |
], | |
], | |
], | |
]; | |
function findParents($node, $searchForName) { | |
if(isset($node['name']) && $node['name'] === $searchForName) { | |
return []; | |
} | |
if(isset($node['children']) && is_array($node['children'])) { | |
foreach ($node['children'] as $treeNode ) { | |
$childResult = findParents($treeNode, $searchForName); | |
if(is_array($childResult)) { | |
array_push($childResult, $treeNode['name']); | |
return $childResult; | |
} | |
} | |
} | |
} | |
/* | |
Array | |
( | |
[0] => 1.3.1 | |
[1] => 1.3 | |
[2] => 1 | |
) | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const datas = { | |
'children': [ | |
{ | |
'name': '1', | |
'children': [ | |
{'name': '1.1'}, | |
{'name': '1.2'}, | |
{ | |
'name': '1.3', | |
'children': [ | |
{'name': '1.3.1'}, | |
{'name': '1.3.2'} | |
] | |
}, | |
{'name': '1.4'} | |
] | |
}, | |
{ | |
'name': '2', | |
'children': [ | |
{'name': '2.1'} | |
] | |
} | |
] | |
}; | |
function findParentsTree({ children = [], ...object }, name) { | |
let result; | |
if (object.name === name) { | |
return object; | |
} | |
return children.some(o => { | |
return result = findParentsTree(o, name) | |
}) && Object.assign({}, object, { children: [result] }); | |
} | |
console.log(findParentsTree(datas, '1.3.2')); | |
/* | |
{ | |
"children": [ | |
{ | |
"name": "1", | |
"children": [ | |
{ | |
"name": "1.3", | |
"children": [ | |
{ | |
"name": "1.3.2" | |
} | |
] | |
} | |
] | |
} | |
] | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed current route and its hierarchical parent when I was working on this in VueJS.