Search Nested Array with String (name) - Familty Tree Array

Viewed 37

I have a big big array with contains names of People across 8 centuries and its quite huge, I want to build a search mechanism, that a user enters name and

  1. should search that nested array
  2. and also from that point all the way up to origin (first name) should get all the names as well.

Currently it just searches first element with .map which is understood. One way is to add nested .map filters but that isn't ideal and number of items are in mulitple hundreds.

Required: Search whole of nested array and return an new array from that point upwards.

Here's the CodeSandbox

1 Answers

You need to iterate your entire tree and walk through each and every sibling to match your search. I have tried javascript and hope this could help you.

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

Related