Home > Blockchain >  Extract the parent node name from Tree who has childrens
Extract the parent node name from Tree who has childrens

Time:12-11

I want to iterate the tree and need to get the id of all the nodes which has the children in string array. while looping it is just returning me the record but doesn't extract the name of the node.

e.g const result = ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY'];

{
  "id": "root",
  "name": "Roles and Permissions",
  "children": [
    {
      "id": "USER",
      "name": "USER",
      "children": [
        {
          "id": "1",
          "name": "VIEW"
        },
        {
          "id": "2",
          "name": "CREATE"
        },
        {
          "id": "3",
          "name": "EDIT"
        }
      ]
    },
    {
      "id": "ROLE",
      "name": "ROLE",
      "children": [
        {
          "id": "8",
          "name": "VIEW"
        },
        {
          "id": "9",
          "name": "CREATE"
        },
        {
          "id": "10",
          "name": "EDIT"
        },
        {
          "id": "11",
          "name": "DELETE"
        }
      ]
    },
    {
      "id": "DASHBOARD",
      "name": "DASHBOARD",
      "children": [
        {
          "id": "BRAND",
          "name": "BRAND",
          "children": [
            {
              "id": "52",
              "name": "VIEW"
            },
            {
              "id": "53",
              "name": "CREATE"
            },
            {
              "id": "54",
              "name": "EDIT"
            },
            {
              "id": "55",
              "name": "DELETE"
            }
          ]
        },
        {
          "id": "COMPANY",
          "name": "COMPANY",
          "children": [
            {
              "id": "56",
              "name": "VIEW"
            },
            {
              "id": "57",
              "name": "CREATE"
            },
            {
              "id": "58",
              "name": "EDIT"
            },
            {
              "id": "59",
              "name": "DELETE"
            }
          ]
        }
      ]
    }
  ]
}

I tried various looping method to get the list, e.g. but not returning the exact name of the node.

function getParent(nodes) {
    if(Array.isArray(nodes.children)) {
      return nodes.children.map((node) => getParent(node));
    }
    return nodes.name;
  }

CodePudding user response:

You can store the resp in an array and return that array.

const q = {
  "id": "root",
  "name": "Roles and Permissions",
  "children": [
    {
      "id": "USER",
      "name": "USER",
      "children": [
        {
          "id": "1",
          "name": "VIEW"
        },
        {
          "id": "2",
          "name": "CREATE"
        },
        {
          "id": "3",
          "name": "EDIT"
        }
      ]
    },
    {
      "id": "ROLE",
      "name": "ROLE",
      "children": [
        {
          "id": "8",
          "name": "VIEW"
        },
        {
          "id": "9",
          "name": "CREATE"
        },
        {
          "id": "10",
          "name": "EDIT"
        },
        {
          "id": "11",
          "name": "DELETE"
        }
      ]
    },
    {
      "id": "DASHBOARD",
      "name": "DASHBOARD",
      "children": [
        {
          "id": "BRAND",
          "name": "BRAND",
          "children": [
            {
              "id": "52",
              "name": "VIEW"
            },
            {
              "id": "53",
              "name": "CREATE"
            },
            {
              "id": "54",
              "name": "EDIT"
            },
            {
              "id": "55",
              "name": "DELETE"
            }
          ]
        },
        {
          "id": "COMPANY",
          "name": "COMPANY",
          "children": [
            {
              "id": "56",
              "name": "VIEW"
            },
            {
              "id": "57",
              "name": "CREATE"
            },
            {
              "id": "58",
              "name": "EDIT"
            },
            {
              "id": "59",
              "name": "DELETE"
            }
          ]
        }
      ]
    }
  ]
}


let result = []
function r(nodes){
  if(Array.isArray(nodes.children)){
    result.push(nodes.name);
    nodes.children.map((c) => r(c))
    return result;
  }
  return result;
}

console.log(r(q))

CodePudding user response:

You can simply use a recursive function. Here ids is an array. You can initialize it before calling the function. Call this function in your getting IDs method.

const getIdFromNodesWithChild = (node) => {
  if (node.children != undefined){
    ids.push(node.id)
    const children_list = node.children
    children_list.forEach( new_child => getIdFromNodesWithChild(new_child))
}}

caller function

const returnIds = (tree) => {
  ids = []
  getIdFromNodesWithChild(tree)
  return (ids) 
}

result : ['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY']

  • Related