FamilyTree = [
{
fullname: "Adam1",
children: [
{
fullname: "Adam2",
children: [
{
fullname: "Adam3",
children: [
{
fullname: "Adam4",
children: [],
},
{
fullname: "Adam5",
children: [
{
fullname: "Adam6",
children: [
{
fullname: "Adam7",
children: [],
},
],
},
{
fullname: "Adam8",
children: [
{
fullname: "Adam9",
children: [
{
fullname: "Adam10",
children: [],
},
{
fullname: "Adam11",
children: [],
},
],
},
{
fullname: "Adam12",
children: [
{
fullname: "Adam13",
children: [],
},
{
fullname: "Adam14",
children: [],
},
{
fullname: "Adam15",
children: [
{
fullname: "Adam16",
children: [],
},
{
fullname: "Adam17",
children: [],
},
{
fullname: "Adam18",
children: [
{
fullname: "Adam19",
children: [],
},
{
fullname: "Adam20",
children: [],
},
{
fullname: "Adam21",
children: [
{
fullname: "Adam22",
children: [],
},
],
},
],
},
{
fullname: "Adam23",
children: [
{
fullname: "Adam24",
children: [
{
fullname: "Adam25",
children: [
{
fullname: "Adam26",
children: [
{
fullname: "Adam27",
children: [
{ fullname: "Adam28" },
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
],
},
];
tree = [];
function isTheItem(item, searchTerm) {
tree.push(item.fullname);
// console.log('item', item, searchTerm);
return item.fullname.toLowerCase() === searchTerm.toLowerCase(); // condition to identify the children
}
function walk(collection, searchTerm) {
return collection.find(item => isTheItem(item, searchTerm) || walk(item.children, searchTerm));
}
function execute(collection, searchTerm) {
tree = []
walk(collection, searchTerm);
}
execute(FamilyTree, 'Adam6');
console.log(tree);