js树结构递归并修改

let arr = [
        {
          key: "数据库",
          title: "数据库",
          isLeaf: false,
          children: [
            {
              key: "mysql",
              title: "mysql",
              isLeaf: false,
              children: [
                {
                  key: "137",
                  title: "QPS",
                  isLeaf: true,
                },
                {
                  key: "146",
                  title: "MySQL进程信息",
                  isLeaf: true,
                },
              ],
            },
            {
              key: "oracle",
              title: "oracle",
              isLeaf: false,
              children: [
                {
                  key: "137",
                  title: "QPS",
                  isLeaf: true,
                },
              ],
            },
          ],
        },
      ];

      function mapTree(org) {
        const haveChildren =
          Array.isArray(org.children) && org.children.length > 0;
        return {
          text: org.title,
          value: org.key,
          children: haveChildren ? org.children.map((r) => mapTree(r)) : [],
        };
      }
      let data = [];
      data = arr.map((org) => mapTree(org));
      console.log(data);

你可能感兴趣的:(原生js,/,jquery,javascript)