Javascript 如何使用函数更改JSON的响应?

Javascript 如何使用函数更改JSON的响应?,javascript,arrays,angular,typescript,angular-material,Javascript,Arrays,Angular,Typescript,Angular Material,我正在调用一个函数buildFileTree,并将其响应保存为常量数据 const data = this.buildFileTree(dataObject, 0); 其中,dataObject是: const dataObject = JSON.parse(TREE_DATA); const TREE_DATA = JSON.stringify([ { Standard: "Food", Category: [ { Nam

我正在调用一个函数
buildFileTree
,并将其响应保存为常量数据

const data = this.buildFileTree(dataObject, 0);
其中,
dataObject
是:

 const dataObject =  JSON.parse(TREE_DATA);
const TREE_DATA = JSON.stringify([
{
    Standard: "Food",
    Category: [
      {
        Name: "Vegetable",
        Tables: [
          {
            Description:
              "The carrot is a simple root vegetable, usually conical or cylindrical in shape.",
            Name: "Carrots"
          },
          {
            Description:
              " tomatoes come in a wide variety of shapes: round, oblate, pear-shaped, torpedo-shaped,",
            Name: "Tomatoes"
          }
        ]
      },
      {
        Name: "Fruits",
        Tables: [
          {
            Description: "Oranges",
            Name: "Spherical shape is of orange"
          },
          {
            Description: "Grapes",
            Name:
              "Grapes are typically an ellipsoid shape resembling a prolate spheroid."
          }
        ]
      }
    ]
  }
]);
树的数据是:

 const dataObject =  JSON.parse(TREE_DATA);
const TREE_DATA = JSON.stringify([
{
    Standard: "Food",
    Category: [
      {
        Name: "Vegetable",
        Tables: [
          {
            Description:
              "The carrot is a simple root vegetable, usually conical or cylindrical in shape.",
            Name: "Carrots"
          },
          {
            Description:
              " tomatoes come in a wide variety of shapes: round, oblate, pear-shaped, torpedo-shaped,",
            Name: "Tomatoes"
          }
        ]
      },
      {
        Name: "Fruits",
        Tables: [
          {
            Description: "Oranges",
            Name: "Spherical shape is of orange"
          },
          {
            Description: "Grapes",
            Name:
              "Grapes are typically an ellipsoid shape resembling a prolate spheroid."
          }
        ]
      }
    ]
  }
]);
而buildFileTree函数是:

buildFileTree(obj: { [key: string]: any }, level: number): FileNode[] {
    return Object.keys(obj).reduce<FileNode[]>((accumulator, key) => {
      const value = obj[key];
      const node = new FileNode();
      node.filename = key;
      if (value != null) {
        if (typeof value === "object") {
          node.children = this.buildFileTree(value, level);
        } else {
          node.type = value;
        }
      }
      return accumulator.concat(node);
    }, []);
  }
}
buildFileTree(obj:{[key:string]:any},level:number):FileNode[]{
返回Object.keys(obj).reduce((累加器,键)=>{
常量值=对象[键];
const node=new FileNode();
node.filename=key;
if(值!=null){
如果(值的类型==“对象”){
node.children=this.buildFileTree(值,级别);
}否则{
node.type=值;
}
}
返回累加器concat(节点);
}, []);
}
}
上述功能的响应是:

buildFileTree(obj: { [key: string]: any }, level: number): FileNode[] {
    return Object.keys(obj).reduce<FileNode[]>((accumulator, key) => {
      const value = obj[key];
      const node = new FileNode();
      node.filename = key;
      if (value != null) {
        if (typeof value === "object") {
          node.children = this.buildFileTree(value, level);
        } else {
          node.type = value;
        }
      }
      return accumulator.concat(node);
    }, []);
  }
}

预期答复:

似乎我必须对buildFileTree函数进行一些更改。我能得到一些帮助吗


当前响应的工作示例:

函数的主要问题是,在工作示例中,您将其应用于Javascript对象,而
树\u数据是Javascript数组

话虽如此,如果对数组使用
Object.keys()
方法,它将返回数组中元素的索引。 例如,
Object.key([10,20,30]);//返回['0'、'1'、'2']

您的
buildFileTree
应该修改如下

//创建一个新的JS对象,其中每个键都是一个“标准”条目,每个值
//是另一个对象,其键为“Category.Name”,数组的值为所有条目的名称
const result=dataObject.reduce((acc1,e1)=>{
acc1[e1.Standard]=e1.Category.reduce((acc2,e2)=>{
acc2[e2.Name]=e2.Tables.map(e=>e.Name);
返回acc2;
}, {})
返回acc1;
}, {});

希望有帮助!如果你不熟悉这些方法,你一定要检查它们

无需将数组转换为json,然后创建所需的结构。相反,直接使用数组
treeData
,迭代每个对象并映射所需的输出,如下所示:

var treeData=[
{
标准:“食品”,
类别:[
{
名称:“蔬菜”,
表:[
{
说明:
“胡萝卜……形状。”,
名称:“胡萝卜”
},
{
说明:
“西红柿来了……鱼雷形,”,
名称:“西红柿”
}
]
},
{
名称:“水果”,
表:[
{
名称:“橙子”,
描述:“球形为橙色”
},
{
名称:“葡萄”,
说明:
“葡萄……球体。”
}
]
}
]
}
];
让数据={};
用于(treeData中的const obj){
常量内部={};
treeData[obj].Category.forEach(e=>{
内部[e.Name]=e.Tables.map(i=>i.Name)
});
数据[treeData[obj].标准]=内部;
}

控制台日志(数据)您是否尝试过forEach或map?谢谢您的建议。我明白你的意思。您能建议typescript文件中需要的所有更改吗。