NodeJS 从具有子节点的树中提取父节点名称

ukqbszuj  于 2022-12-12  发布在  Node.js
关注(0)|答案(2)|浏览(149)

我想迭代树,并需要获得所有节点的id,这些节点的子节点是字符串数组。当循环时,它只返回记录,但不提取节点的名称。
例如const result =[“根”,“用户”,“角色”,“ Jmeter 板",”品牌“,”公司“];

{
  "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"
            }
          ]
        }
      ]
    }
  ]
}

我尝试了各种循环方法来获取列表,例如,但没有返回节点的确切名称。

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

4si2a6ki1#

可以将resp存储在数组中并返回该数组。

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

bvuwiixz2#

你可以简单地使用一个递归函数。这里ids是一个数组。你可以在调用函数之前初始化它。在你的获取ID方法中调用这个函数。

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

调用函数

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

结果:['root', 'USER', 'ROLE', 'DASHBOARD', 'BRAND', 'COMPANY']

相关问题