Typescript 我从服务器获得了JSON,如下所示:

Typescript 我从服务器获得了JSON,如下所示:,typescript,Typescript,现在我想把这个JSON转换成 [ { "id":"one", "state":"two", "url":"www.google.com", "childs":[ "one", "two", "three", "four" ] }, { "id":"two", "state":"three", "url":"www.yahoo.com", "child

现在我想把这个JSON转换成

[
  {
    "id":"one",
    "state":"two",
    "url":"www.google.com",
    "childs":[
        "one",
        "two",
        "three",
        "four"
    ]
  },
  {
    "id":"two",
    "state":"three",
    "url":"www.yahoo.com",
    "childs":[
        "one",
        "two",
        "three",
        "four"
    ]
  }
]
我使用JS获得如下结果:

[  
              {  
                "name":"id",
                "children":[  
                    {  
                      "name":"one"
                    }
                ]
              },
              {  
                "name":"state",
                "children":[  
                    {  
                      "name":"two"
                    }
                ]
              },
              {  
                "name":"url",
                "children":[  
                    {  
                      "name":"www.google.com"
                    }
                ]
              },
              {  
                "name":"childs",
                "children":[  
                    {  
                      "name":"one"
                    },
                    {  
                      "name":"two"
                    },
                    {  
                      "name":"three"
                    },
                    {  
                      "name":"four"
                    }
                ]
              }

          ]
有人能给我建议一下我们如何通过子儿童进行循环吗?提前感谢

您可以使用

Object.entries提供arr上每个元素的键和值。 映射每个条目并检查值是字符串还是数组。相应地添加子项。 将值推送到op 设arr=[{id:one,state:two,url:www.google.com,childs:[one,two,three,four]},{id:two,state:two,url:www.yahoo.com,childs:[one,two,three,four]}] 设op=arr.reduceop,inp=>{ 设temp=Object.entriesinp.map[key,value]=>{name:key,children:Array.isArrayvalue?value.mape=>{name:e}:[{name:value}]} 操作推送…温度 返回操作 },[]
console.logop此函数应在嵌套任意深度的内容上运行:

常数is=T=>val=>val!=null&&val.constructor==T | | val instanceof T; 常量转换=x=> 是数组x吗 ? x.映射变换 :是对象x吗 ? Object.entries x.map [名称,val]=>{ 名称 儿童:transformval } :{name:x} const data=[{id:one,state:two,url:www.google.com,childs:[one,two,three,foo:{bar:baz,quz:corg},url:www.yahoo.com,childs:[one,two,three,three]}] console.logtransformdata
。作为控制台包装器:{height:100%!important}请签出,否则您应该能够通过嵌套这些循环来循环子级。您的数据是否也嵌套得更深?例如,对象是否也包含值也是对象的属性?还是只有字符串和字符串数组?@Vicky总是乐于助人:如果有帮助,你可以将其标记为正确答案
buildChildrenNodes() {
    let arrayOneStrings: Array<object> = [];
    this.treedata.map((element: any) => {
        const sortedData = Object.entries(element);
        let arrayTwoStrings: Array<object> = [];
        for (const [key, value] of sortedData) {
            const twoElement =
            {
                name: key,
                children: [
                    {
                        name: value
                    }
                ]
            }
            arrayTwoStrings.push(twoElement);
        }
        const oneElement = {
            name: element.id,
            children: arrayTwoStrings
        };
        arrayOneStrings.push(oneElement);
    });
    this.formattedTreeData = arrayOneStrings;
};