繁体   English   中英

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

[英]how to encode/ change the format of existing json to another using angular/javascript

这里我有以下格式的json

目前格式

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

        }
      }
    },
    "link": {

    },
    "name": {

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

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

        }

      }
    }
  }
}

我正在使用某些插件,例如ngx-tree,primeng插件,其数据需要采用以下不同格式

必填格式

  [
        {
          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
  }

请确保使用recursiveConvert(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 => ({ name: k, children: json.info[k].properties ? Object.keys(json.info[k].properties).map(kk => ({name: kk})) : [] })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

这是一个单行的纯功能解决方案:

 const input = { "info": { "laptop": { }, "config": { "properties": { "ram": { }, "processor": { }, "hdd": { } } }, "link": { }, "name": { }, "company": { "properties": { "model": { "properties": { "apple": {}, "banana": {}, "pear": {} } }, "maker": { "type": "integer" }, "country": { "type": "text" }, "enterprise": { } } } } } const convert = input => Object .entries(input) .map(([name, value]) => ({ name, children: convert(value.properties || {}) })) console.log(convert(input.info)); 

这是es2015版本:

function convert(input) {
  return Object.keys(input).map(function(name) {
    return {
      name: name,
      children: convert(input[name].properties || {})
    };
  });
}

暂无
暂无

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

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