Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用angular/javascript将现有json的格式编码/更改为其他格式_Javascript_Angular - Fatal编程技术网

如何使用angular/javascript将现有json的格式编码/更改为其他格式

如何使用angular/javascript将现有json的格式编码/更改为其他格式,javascript,angular,Javascript,Angular,这里我有一个json,格式如下 当前格式 { "info": { "laptop": { }, "config": { "properties": { "ram": { }, "processor": { }, "hdd": { } } }, "link": { }, "name": { },

这里我有一个json,格式如下

当前格式

 {
  "info": {
    "laptop": {
    },
    "config": {
      "properties": {
        "ram": {
        },
        "processor": {
        },
        "hdd": {

        }
      }
    },
    "link": {

    },
    "name": {

    },
    "company": {
      "properties": {
        "model": {

        },
        "maker": {
          "type": "integer"
        },
        "country": {
          "type": "text"
        },
        "enterprise": {

        }

      }
    }
  }
}
我正在使用一些插件,比如ngx tree,这些插件的数据格式需要不同,如下所示

所需格式

  [
        {
          name: 'laptop',

        },
        {
          name: 'config',
          children: [
            { name: 'ram', children: [] },
            { name: 'processor' },
            {name:'hdd'}
          ]
        },
         {
          name: 'link',
          children: []
        },
        {
          name: 'name',
          children: []
        },

        {
          name: 'company',
          children: [
            { name: 'model' },
            { name: 'maker' },
            { name: 'country' },
            { name: 'enterprise' }

          ]
        }
      ];

现在我的问题是如何将数据从当前格式更改为所需格式?有什么建议可以让我进行更改吗?

您可以递归地遍历输入对象,将其映射到所需的格式,如下所示:

  function recursiveConvert(input) {
     let converted = []

     for(i in input){
       converted.push({
         name: i,
         children: []
       })

       if(input[i].properties) {
          converted[converted.length - 1].children.push(recursiveConvert(input[i].properties)) 
       }
     }

     return converted
  }
如果不需要空的子数组,只需将其更改为:

function recursiveConvert(input) {
     let converted = []

     for(i in input){
       converted.push({
         name: i
       })

       if(input[i].properties) {
          let lastInsert = converted[converted.length - 1]

          lastInsert.children = [recursiveConvert(input[i].properties)]
       }
     }

     return converted
  }
请务必使用递归转换(myInput.info)调用它,因为它不需要包装器对象。

您可以执行以下操作:

const json={“info”:{“laptop”:{},“config”:{“properties”:{“ram”:{},“processor”:{},“hdd”:{}},“link”:{},“name”:{},“company”:{“properties”:{“model”:{},“maker”:{“type”:“integer”},“country”:{“type”:“text”},“enterprise”:{};
const result=Object.keys(json.info).map(k=>({
姓名:k,,
子项:json.info[k].properties
?Object.keys(json.info[k].properties).map(kk=>({name:kk}))
: []
}));
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
这里是一个单行的纯功能解决方案:

const输入={
“信息”:{
“笔记本电脑”:{
},
“配置”:{
“财产”:{
“ram”:{
},
“处理器”:{
},
“硬盘驱动器”:{
}
}
},
“链接”:{
},
“姓名”:{
},
“公司”:{
“财产”:{
“模型”:{
“财产”:{
“苹果”:{},
“香蕉”:{},
“梨”:{}
}
},
“制造者”:{
“类型”:“整数”
},
“国家”:{
“类型”:“文本”
},
“企业”:{
}
}
}
}
}
const convert=input=>Object
.条目(输入)
.map([name,value])=>({
名称
子级:转换(value.properties | |{})
}))

console.log(convert(input.info))
你能更新所需的格式吗?@Harish现在它已经更新了请检查基于我们的输入我创建了这个堆栈链接请检查但我不能像我说的那样获取任何日志用这个调用它。recursiveConvert(this.data.info),它不需要包装信息对象它能在n号子对象上工作吗?我的意思是,假设一个键有嵌套的子项,我们也有嵌套的子项,你可以通过跟踪当前对象并检查是否有子项来递归地执行。我的意思是像这样{name:'root2',子项:[{name:'child1'},{name:'child2',子项:[{name:'grand1'},{name:'grande2'}]}]}];?它能在n个数字的子项上工作吗?我的意思是假设一个键有嵌套的子项,并且我们也有嵌套的子项。它使用es 2017对吗?在我的应用程序中,es 6或5 bcoz是否可能出现错误