简体   繁体   English

从嵌套数组到对象的json转换

[英]json conversion from nested array to object

I have json nested array as follows:我有如下 json 嵌套数组:

 Parent: {
      Child1: [
        {name:'grandchild1', value:'abc', checked:true},
        {name:'grandchild2', value:'pqr', checked:false}
      ],
      Child2: [
        {name:'grandchild3', value:'abcd', checked:false},
        {name:'grandchild4', value:'pqrs', checked:true}
      ],
parent2{...........}....
    };

I need to convert it into:我需要将其转换为:

 [  
   {  
      "filename":"Parent",
      "children":[  
         {  
            "filename":"Child1",
            "children":[  
               {  
                  "filename":"grandchild1",
                  "type":"ts"
               },
               {  
                  "filename":"grandchild2",
                  "type":"ts"
               }
            ]
         },
         {  
            "filename":"Child2",
            "children":[  
               {  
                  "filename":"grandchild3",
                  "type":"ts"
               },
               {  
                  "filename":"grandchild4",
                  "type":"ts"
               }
            ]
         }
      ]
   },
   { filename:"Parent1"..........
   },....
]

It is a part of angular material tree.它是角度材料树的一部分。 They have sample code Link他们有示例代码链接

Tried with below code:尝试使用以下代码:

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 + 1);
        } else {
          node.type = value;
        }
      }

      return accumulator.concat(node);
    }, []);
  }

But not getting what I need.但没有得到我需要的东西。

Please suggest me how can I convert into the following format so that it will accept the corresponding way?请建议我如何转换为以下格式,以便它接受相应的方式?

You can use the for of and for in functions您可以使用for offor in函数

 const list = {Parent1 :{ Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ] , Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ] }, Parent2 :{ Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ] , Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ] }}; const res = [] for(let parent in list){ let parentTemp = { filename : parent, children : [] } for(let child in list[parent]){ let childTemp = {filename : child, children : []}; for(let grandChild of list[parent][child]){ childTemp.children.push({filename : grandChild.name, type : grandChild.value, status: grandChild.checked}); } parentTemp.children.push(childTemp); } res.push(parentTemp); } console.log(res);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM