Search code examples
arraysangularcomparisonhierarchy

I want to get Hierarchy from an Array - Angular 8


I have an Array and One String Value. I want to get the Hierarchy of the String into an Array.

For example, I have a string value "Casuals". "Casuals" value is inside the "Shirts" object. "Shirts" value is inside the "Men" object. And "Men" value is inside the "Default Category" object. So, this is how the logic should be work.

Here is my Sample Array:

{
  "id": 2,
  "name": "Default Category",
  "children_data": [
    {
      "id": 3,
      "name": "Men",
      "children_data": [
        {
          "id": 11,
          "name": "T-Shirts",
          "children_data": [
            {
              "id": 27,
              "name": "Polos"
            },
            {
              "id": 28,
              "name": "Tees"
            }
          ]
        },
        {
          "id": 12,
          "name": "Shirts",
          "children_data": [
            {
              "id": 30,
              "name": "Casuals"
            },
            {
              "id": 31,
              "name": "Formals"
            }
          ]
        }
      ]
    },
    {
      "id": 4,
      "name": "Women",
      "children_data": [
        {
          "id": 80,
          "name": "Western wears",
          "children_data": [
            {
              "id": 81,
              "name": "T-Shirts"
            },
            {
              "id": 82,
              "name": "Tank & Crop Tops"
            }
          ]
        },
        {
          "id": 21,
          "name": "Ethnic wears",
          "children_data": [
            {
              "id": 51,
              "name": "Kurta & Kurtis"
            },
            {
              "id": 52,
              "name": "Kurta Sets"
            }
          ]
       }
      ]
    }
  ]
}

And I have the value

let myCategory = "Casuals";

So, that I want to get my final value is ["Default Category", "Men", "Shirts", "Casuals"]

I'm still struggling to get the Hierarchy of the value.


Solution

  • Please try below code for your problem. Let me know If you are facing any issue. Please see working demo.

    Call getFilterdObject(data,'Polos'), data is your object.

    function getFilterdObject(obj,param){
      let finalArray =[];  
      finalArray.push(obj.name);  
      if(obj['name'] != param && obj['children_data']){
        let filterData = obj['children_data'].filter(function search(a) {
            var children;
            if (a.name === param) {
                return true;
            }
            if (!Array.isArray(a.children_data)) {
                return false;
            }
            children = a.children_data.filter(search);
            if (children.length) {
                a.children_data = children;
                return true; 
            } 
        });
        if(filterData.length){
          getArray(filterData, param);
        }
        else{
          finalArray =[];
        }
      }
    
      function getArray(obj,param){
        if(obj.length){     
          obj.map((val)=>{  
            finalArray.push(val.name);
            if(val.children_data && val.name != param){
              getArray(val.children_data, param);  
            }
          }); 
        }  
     }
       return finalArray;
    };