码迷,mamicode.com
首页 > Web开发 > 详细

获取json中的属性值,生成新json

时间:2020-12-11 12:26:45      阅读:11      评论:0      收藏:0      [点我收藏+]

标签:ret   cal   获取   item   prim   body   bsp   func   UNC   

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 
  4 <head>
  5     <meta charset="UTF-8">
  6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7     <title>Document</title>
  8 </head>
  9 
 10 <body>
 11     <script>
 12         var originData = [
 13             {
 14                 "DepartmentId": 1,
 15                 "DepartmentName": "技术部",
 16                 "SubDepartments": [],
 17                 "PrimaryKey": 0
 18             },
 19             {
 20                 "DepartmentId": 2,
 21                 "DepartmentName": "销售中心",
 22                 "SubDepartments": [
 23                     {
 24                         "SubDepartmentId": 1,
 25                         "DepartmentId": 2,
 26                         "SubDepartmentName": "某某系列1",
 27                         "Teams": [
 28                             {
 29                                 "TeamId": 1,
 30                                 "SubDepartmentId": 1,
 31                                 "DepartmentId": 2,
 32                                 "TeamName": "某某团队1",
 33                                 "PrimaryKey": 0
 34                             },
 35                             {
 36                                 "TeamId": 2,
 37                                 "SubDepartmentId": 1,
 38                                 "DepartmentId": 2,
 39                                 "TeamName": "某某团队2",
 40                                 "PrimaryKey": 0
 41                             }
 42                         ],
 43                         "PrimaryKey": 0
 44                     },
 45                     {
 46                         "SubDepartmentId": 2,
 47                         "DepartmentId": 2,
 48                         "SubDepartmentName": "某某系列2",
 49                         "Teams": [
 50                             {
 51                                 "TeamId": 3,
 52                                 "SubDepartmentId": 2,
 53                                 "DepartmentId": 2,
 54                                 "TeamName": "某某团队3",
 55                                 "PrimaryKey": 0
 56                             }
 57                         ],
 58                         "PrimaryKey": 0
 59                     }
 60                 ],
 61                 "PrimaryKey": 0
 62             }
 63         ];
 64 
 65         function treeConvert(data, idPre, lv) {
 66             var result = [],
 67                 idPre = idPre || ‘‘,
 68                 lv = lv || 1;
 69             // console.log(arguments);
 70             //传过来的json中要获取值的属性名称;
 71             var idKeys = [‘DepartmentId‘, ‘SubDepartmentId‘, ‘TeamId‘];
 72             var nameKeys = [‘DepartmentName‘, ‘SubDepartmentName‘, ‘TeamName‘]
 73             var childrenKeys = [‘SubDepartments‘, ‘Teams‘, false];
 74 
 75             var curr = lv - 1,
 76                 idKey = idKeys[curr],
 77                 nameKey = nameKeys[curr],
 78                 childrenKey = childrenKeys[curr]
 79 
 80             data.forEach(function (ele, idx) {
 81                 // console.log(ele);
 82                 var item = {
 83                     id: idPre + ‘|‘ + ele[idKey],
 84                     orgId: ele[idKey],
 85                     name: ele[nameKeys]
 86                 };
 87                 console.log(‘item‘,item);
 88                 if (childrenKey && ele[childrenKey] && ele[childrenKey].length) {
 89                     console.log("go")
 90                     item.children = [];
 91                     item.children.push(treeConvert(ele[childrenKey], item.id, lv + 1));
 92                 }
 93                 console.log("result1",result);
 94                 result.push(item);
 95             });
 96             console.log("result2",result);
 97             return result;            
 98         }
 99         console.log("new_originData",treeConvert(originData));
100 
101     </script>
102 
103 </body>
104 
105 </html>

 

获取json中的属性值,生成新json

标签:ret   cal   获取   item   prim   body   bsp   func   UNC   

原文地址:https://www.cnblogs.com/wuzhaomao/p/14097758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!