• 无限级修改JSON里边的key


    var tree = [{
      "PositionGuid": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
      "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
      "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2b",
      "DevName": "AXIS编码器",
      "Parent": "eee8aba0-d642-4e17-89d6-19951e10e042",
      "DevLevel": 2,
      "Children": [{
        "PositionGuid": "b849c03f-7cdd-4ebf-bf12-109da72f867e",
        "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
        "DevNameID": "11",
        "DevName": "配电柜",
        "Parent": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
        "DevLevel": 3,
      "Children": [{
        "PositionGuid": "ef8da101-a2c9-4d94-b109-4381dc7bf753",
        "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
        "DevNameID": "12",
        "DevName": "配电箱",
        "Parent": "b849c03f-7cdd-4ebf-bf12-109da72f867e",
        "DevLevel": 4,
      "Children": [{
        "PositionGuid": "f6cfbb8e-97d6-4424-9abd-3d1c85c53bd3",
        "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
        "DevNameID": "123",
        "DevName": "气象站",
        "Parent": "ef8da101-a2c9-4d94-b109-4381dc7bf753",
        "DevLevel": 5,
        "Children": []
        }]
      }]
     }, {
      "PositionGuid": "c85a24f4-0f7e-47e2-bfb6-f3f761eb3e55",
      "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
      "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2c",
      "DevName": "UPS电源",
      "Parent": "f5af03a9-968c-46b1-8b5d-17d8a64be22d",
      "DevLevel": 3,
      "Children": [{
        "PositionGuid": "5caa3051-3e7b-4063-80e5-34612f00d1f5",
        "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
        "DevNameID": "0010d1d6-2fc1-471c-a82a-3ddb5248de2e",
        "DevName": "安防摄像机",
        "Parent": "c85a24f4-0f7e-47e2-bfb6-f3f761eb3e55",
        "DevLevel": 4,
        "Children": [{
        "PositionGuid": "db81bf26-f15a-48ca-a12a-8a954299b637",
        "TemplateGuid": "e5d53c43-a77b-419d-8b49-4d32deff14fd",
        "DevNameID": "1",
        "DevName": "安防线缆",
        "Parent": "5caa3051-3e7b-4063-80e5-34612f00d1f5",
        "DevLevel": 5,
        "Children": []
      }]
      }]
      }]
    }];
    //先定好修改的key
    var key = 'children'
    //arr 接受的json
    function parseJson(arr) {
      arr = arr.slice()
      function toParse(arr) {
        console.log(arr,'====');
        arr.forEach(function (item) {
        if (item.Children && Array.isArray(item.Children)) {
          item[key] = item.Children
          toParse(item[key])
        }
        delete item.Children  
      })
      return arr
      }
      return toParse(arr)
    }
    console.log(parseJson(tree))
  • 相关阅读:
    [Leetcode]@python 89. Gray Code
    [Leetcode]@python 88. Merge Sorted Array.py
    [Leetcode]@python 87. Scramble String.py
    [Leetcode]@python 86. Partition List.py
    [leetcode]@python 85. Maximal Rectangle
    0523BOM
    0522作业星座
    0522dom
    0520
    0519作业
  • 原文地址:https://www.cnblogs.com/wgy0528/p/11206655.html
Copyright © 2020-2023  润新知