'Complex top level parents by param of the lowest child Saving your hierarchy and without repeating his hierarchy

Good evening. Today I am looking for a recursive function that will help me find all the greatest parents of a child of an element of a multidimensional array of objects. Saving your hierarchy and without repeating his hierarchy

const $tables = [{
        "name": "years",
        "parents": []
      },
      {
        "name": "grades",
        "parents": []
      },
      {
        "name": "groups",
        "parents": ["grades"]
      },
      {
        "name": "areas",
        "parents": []
      },
      {
        "name": "subjects",
        "parents": ["areas"]
      },
      {
        "name": "nivels",
        "parents": ["subjects"]
      },
      {
        "name": "users",
        "parents": []
      },
      {
        "name": "assessment_types",
        "parents": ["years", "groups", "subjects", "nivels"]
      },
      {
        "name": "assessments",
        "parents": ["assessment_types", "users"]
      }
    ];

    function getRelativeParents($search, $exclude = []) {
      $finish = false;
      $relatives = [];
      while ($finish == false) {
        $current_table = $tables.find($table => $table.name == $search);
        if ($current_table.parents.length > 0) {
          $search = $current_table.parents[0]; //I  know that in this line i need a recursive funtion but i do not how make
          $relatives.unshift($search);
        } else {
          $finish = true;
        }
      }
      return $relatives;
    }


    for (item in $tables) {
      if ($tables.some(table => table.name == $tables[item]["name"])) {
        $tables[item]["all_relatives_parents"] = getRelativeParents($tables[item]["name"]);
      }
    }

    console.log($tables);

The result I need is the following, if you can see in the field all_relatives_parents we store the hierarchy of each parent according to the order of the parents specified in the parents attribute, the complex part is in the parents that have multiple parents , as you can see in the object called assessments, it has two parents, and those parents like assessment_types have multiple parents, so I need to store their pecking order in the all_relatives_parents attribute

$results = [
          {"name" : "years" , "parents":[],"all_relatives_parents" :[]},
          {"name" : "grades" , "parents":[],"all_relatives_parents" :[]},
          {"name" : "groups" , "parents":["grades"],"all_relatives_parents" :["grades"]},
          {"name" : "areas" , "parents":[],"all_relatives_parents" :[]},
          {"name" : "subjects" , "parents":["areas"],"all_relatives_parents" :["areas"]},
          {"name" : "nivels" , "parents":["subjects"],"all_relatives_parents" : ["areas","subjects"]},
          {"name" : "users" , "parents":[],"all_relatives_parents" :[]},
          {"name" : "assessment_types" , "parents":["years","groups","subjects","nivels"], "all_relatives_parents": ["years","grades","groups","areas","subjects","nivels"]},
          {"name" : "assessments" , "parents":["assessment_types","users"],"all_relatives_parents" :["assessment_types","years","grades","groups","areas","subjects","nivels","users"]}
      ];

Now I am going to explain why that is the reason for the results I need according to the most complex result for this case for the object assessments which is the one with the most parents at higher levels

      "assessment_types" has the next parents
          [
            "years" : not has parents the result is empty [] If we join it to its child and keep the hierarchy the result is ["years"]
            "groups" : has a parent and the result is ["grades"] If we join it to its child and keep the hierarchy ["grades", "groups"]
            "subjects": has a parent and the result is ["areas"] If we join it to its child and keep the hierarchy ["areas", "subject"]
            "nivels" : has a parent and the result is ["areas", "subject"]  If we join it to its child and keep the hierarchy ["areas", "subject", "nivel"] 
      ] if the values are not repeated and all the results are joined, the hierarchy that must be stored is ["years","grades","groups","areas","subjects","nivels","users"] If we join it to its child and keep the hierarchy the result is ["assessment_types","years","grades","groups","areas","subjects","nivels"]

  "users" : not has parents the result is empty [] If we join it to its child and keep the hierarchy ["users"]

  joining the parents of the object with the property assessments the hierarchy that must be stored is this ["assessment_types","years","grades","groups","areas","subjects","nivels","users"]

I really appreciate any help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source