是否可以仅从JSON显示特定属性?

时间:2019-12-03 14:04:34

标签: json angular tree

我正在查看此页面: https://material.angular.io/components/tree/examples  与Angular树示例一起使用,并希望将其与复选框一起使用。我遇到的问题是我必须以给定格式填充树的JSON:

const TREE_DATA = {
   "tableOfContents":[
      {
         "id":"{039CCB54-97EB-4C64-80E0-B3D44482172B}",
         "caption":"Overview",
         "file":"hlpIntroduction",
         "iconIndex":-1,
         "children":[

         ]
      },      
      {
         "id":"{9204FD14-7841-40B9-A0DA-8B80E9EB0152}",
         "caption":"Account Settings",
         "file":null,
         "iconIndex":-1,
         "children":[
            {
               "id":"{BFB2F082-981A-4E0B-A122-5320B868B57B}",
               "caption":"My Account",
               "file":"hlpCMStartPage",
               "iconIndex":-1,
               "children":[

               ]
            }
         ]
      }
  ]
};

这显示所有属性。在此示例中,我希望将“概述”和“帐户设置”显示为第一级节点(在tableOfContents下),将“我的帐户”显示为第二级节点-“帐户设置”的子级。因此,只有“标题”值会显示在树中。

我一直在研究buildFileTree方法,但无法弄清楚如何更改它来实现我在这里需要的功能。

这可能吗?

谢谢

1 个答案:

答案 0 :(得分:2)

尝试这样:

this.displayData = this.TREE_DATA.tableOfContents.map(item => ({
  caption: item.caption,
  children: item.children.map(y => ({ caption:y.caption}))
}));

Demo