将 数组 转换 为 嵌套 JSON 对象 - Angular 材质 树

tp5buhyn  于 2022-11-19  发布在  Angular
关注(0)|答案(2)|浏览(153)

我正在拼命地尝试从JSON嵌套格式的角树中获取所选节点。到目前为止,我已经成功地获取了this.checklistSelection.selected的平面节点的所选数组。但我需要的是,我需要以JSON格式获取所选节点,其中包含所有嵌套的JSON对象的级别。

[{item: "Risk Analysis", level: 0, expandable: true}
,{item: "Standard", level: 1, expandable: true}
,{item: "Active", level: 2, expandable: true}
,{item: "Volatility", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}
,{item: "VaR (95%, 2 weeks, Chebyshev)", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}
,{item: "Benchmark", level: 2, expandable: true}
,{item: "Volatility", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}
,{item: "VaR (95%, 2 weeks, Chebyshev)", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}
,{item: "Portfolio", level: 2, expandable: true}
,{item: "Volatility", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}
,{item: "VaR (95%, 2 weeks, Chebyshev)", level: 3, expandable: true}
,{item: "Contribution", level: 4, expandable: true}
,{item: "Total", level: 5, expandable: false}
,{item: "Systematic", level: 5, expandable: false}
,{item: "Specific", level: 5, expandable: false}]

预期值:

"Risk Analysis": {
      "Standard": {
        "Active": {
          "Volatility": {
            "Contribution": ["Total", "Systematic", "Specific"]
          },
          "VaR (95%, 2 weeks, Chebyshev)": {
            "Contribution": ["Total", "Systematic", "Specific"]
          }
        },
        "Portfolio": {
          "Volatility": {
            "Contribution": ["Total", "Systematic", "Specific"]
          },
          "VaR (95%, 2 weeks, Chebyshev)": {
            "Contribution": ["Total", "Systematic", "Specific"]
          }
        },
        "Benchmark": {
          "Volatility": {
            "Contribution": ["Total", "Systematic", "Specific"]
          },
          "VaR (95%, 2 weeks, Chebyshev)": {
            "Contribution": ["Total", "Systematic", "Specific"]
          }
        }
      }
    }
  }

有人能告诉我是否有Mat树提供的方法,或者任何一种函数可以做到这一点吗?
提前感谢:)

bfhwhh0e

bfhwhh0e1#

若要建立树状目录,您必须预先行程数据,为每个项目指派识别码。您可以使用堆栈来追踪指派时的相关性。
您可以分阶段完成此操作:
1.为每个项目指定idparentId键(applyRelationships
1.将平面数组转换为树(listToTree
1.将树转换为对象(treeToObject
在原始示例中,我通过设置max-depth来强制嵌套每个对象。我没有使用expandable属性。在这个修改后的示例中,我丢弃了maxDepth参数。
第一个

原始响应

第一次

tvokkenx

tvokkenx2#

Angular 材质树到JSON对象转换器。

export class TodoItemNode {
  children: TodoItemNode[];
  item: string;
}

treeToObject(result: any, nodes: TodoItemNode[]): any {
 for (const node of nodes) {
  if (node.children && node.children.length > 0) {
    let isArray = true;
    for (const child of node.children) {
      if (child.children && child.children.length > 0) {
        isArray = false;
        break;
      }
    }
    if (isArray) {
      result[node.item] = [];
      for (const child of node.children) {
        result[node.item].push(child.item);
      }
    } else {
      result[node.item] = {};
      this.treeToObject(result[node.item], node.children);
    }
  } else {
    result[node.item] = null;
  }
 }
 return result;
}

相关问题