如何创建像树一样的预期对象

时间:2018-02-15 06:30:36

标签: javascript

我正在尝试使用以下对象

创建输出对象
[{
  "department_name": "Education",
  "sub_department_name": [{
    "name": "E1",
    "app": [{
      "name": "app1",
      "date": "2018-02-06T18:30:00.000Z"
    }]
  }, {
    "name": "E2",
    "app": [{
      "name": "app2",
      "date": "2018-02-13T06:38:02.109Z"
    }, {
      "name": "app3",
      "date": "2018-02-13T06:38:50.012Z"
    }]
  }]
}];

最终输出应如下所示

[
  {
    "label": "root",
    "children": [
      {
        "label": "Education",
        "collapsed": true,
        "children": [
          {
            "label": "E1",
            "collapsed": true,
            "children": [
              {
                "label": "app1",
                "collapsed": true
              }
            ]
          },
          {
            "label": "E2",
            "collapsed": true,
            "children": [
              {
                "label": "app2",
                "collapsed": true
              },
              {
                "label": "app3",
                "collapsed": true
              }
            ]
          }
        ]
      }
    ]
  }
]

我尝试过使用JavaScript方法,但无法实现预期的输出。任何有关这方面的帮助都会非常有用。

JSFIDDLE

根据要求,在此处发布我的代码

var obj = [{
  "department_name": "Education",
  "sub_department_name": [{
    "name": "E1",
    "app": [{
      "name": "app1",
      "date": "2018-02-06T18:30:00.000Z"
    }]
  }, {
    "name": "E2",
    "app": [{
      "name": "app2",
      "date": "2018-02-13T06:38:02.109Z"
    }, {
      "name": "app3",
      "date": "2018-02-13T06:38:50.012Z"
    }]
  }]
}];
var deptObj = {};
var subMainObj = {};

var subChildrenArr = [];

var applnArr = [];
for (x in obj) {
  deptObj.label = obj[x].department_name;
  for (y in obj[x].sub_department_name) {
    var subDeptObj = {};
    subDeptObj.label = obj[x].sub_department_name[y].name;
    subChildrenArr.push(subDeptObj);
    for (z in obj[x].sub_department_name[y].app) {
      var applnObj = {};
      applnObj.label = obj[x].sub_department_name[y].app[z].name;
      applnArr.push(applnObj);
      subDeptObj.children = applnArr;

    }

  }
}

deptObj.children = subChildrenArr;
var deptArr = deptObj;
var root = {};
root.label = "root";
root.children = deptArr;
alert(JSON.stringify(root));

1 个答案:

答案 0 :(得分:1)

您可以使用.map

let json = [{
  "department_name": "Education",
  "sub_department_name": [{
    "name": "E1",
    "app": [{
      "name": "app1",
      "date": "2018-02-06T18:30:00.000Z"
    }]
  }, {
    "name": "E2",
    "app": [{
      "name": "app2",
      "date": "2018-02-13T06:38:02.109Z"
    }, {
      "name": "app3",
      "date": "2018-02-13T06:38:50.012Z"
    }]
  }]
}, {
  "department_name": "Test",
  "sub_department_name": [{
    "name": "T1",
    "app": [{
      "name": "app1",
      "date": "2018-02-06T18:30:00.000Z"
    }]
  }]
}];

//Contruct the children
let children = json.map(o => {
  let child = o.sub_department_name.map(v => {
    let c = v.app.map(x => { return { "label": x.name, "collapsed": true }});
    return {
      "label": v.name,
      "collapsed": true,
      "children": c
    }
  });

  return {
    "label": o.department_name,
    "collapsed": true,
    "children": child
  };
});

//Contruct the final array
let result = [{
  "label": "root",
  "children": children
}];

console.log(result);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map