将一组平面记录(使用父键)转换为嵌套结构?

时间:2012-09-05 17:33:59

标签: javascript for-loop

我需要一个JS循环来将对象数组更改为json对象。以下是对象的示例数组:

var employees= [
  {name:"Jeff",manager:null,title:"The Pres"},
  {name:"Amy",manager:"Aaron",title:"lll"},
  {name:"Chris",manager:"Aaron",title:"lll"},
  {name:"Dana",manager:"Aaron",title:"lll"},
  {name:"Joe",manager:"Aaron",title:"lll"},
  {name:"Mike",manager:"Aaron",title:"lll"},
  {name:"Aaron",manager:"Jeff",title:"lll"}
];

这是预期的json对象:

var json = {
    name:"Jeff",
    title:"The Pres",
    children:[{
        name:"Aaron",
        title:"some title",
        children[
            {name:"Amy",title:"some title",children[]},
            {name:"Chris",title:"some title",children:[]},
            {name:"Dana",title:"some title",children:[]},
            {name:"Joe",title:"some title",children:[]},
            {name:"Mike",title:"some title",children:[]
        }]
    }]
};

但它超出了我的范围。

2 个答案:

答案 0 :(得分:5)

var employees= [
  {name:"Jeff",manager:null,title:"The Pres"},
  {name:"Amy",manager:"Aaron",title:"lll"},
  {name:"Chris",manager:"Aaron",title:"lll"},
  {name:"Dana",manager:"Aaron",title:"lll"},
  {name:"Joe",manager:"Aaron",title:"lll"},
  {name:"Mike",manager:"Aaron",title:"lll"},
  {name:"Aaron",manager:"Jeff",title:"lll"}
];

​var hash = {};
var root;

for (var i = 0; i < employees.length; i++) {
    var employee = employees[i];
    hash[employee.name] = {
        name:employee.name, manager:employee.manager, title:employee.title, children: []
    };
}
for (var i = 0; i < employees.length; i++) {
    var employee = hash[employees[i].name];
    var manager = hash[employee.manager];
    if (manager) {
        manager.children.push(employee);
    } else {
        root = employee;
    }
}
// root is the employee without a manager

JsFiddle

答案 1 :(得分:2)

这就是我写它的方式。请注意,Big-O的界限明显大于Onchie发布的答案(大约O(n^2) vs O(n))。但是,对于较小的数据集,这是无关紧要的,我发现这种方法更容易理解。

首先,我会有一个简单的功能,对于任何经理,都会告诉我直接雇员是谁。 (请注意,这只是 标准filter函数,而ECMAScript 5jQuery都提供了更高阶的函数,可以使用更干净,更有竞争力的代码。)

function employeesForManager(employees, managerName) {
    var res = []
    for (var i = 0; i < employees.length; i++) {
        if (employees[i].manager == managerName) {
            res.push(employees[i])
        }
    }
    return res
}

然后我将创建一个递归函数,它遍历树并构建每个级别。 (同样,这里的循环可以用简单的map函数替换。)

function giveShape(employees, manager) {
    var immediateEmployees = employeesForManager(employees, manager.name)
    var children = []
    for (var i = 0; i < immediateEmployees.length; i++) {
        children.push(giveShape(employees, immediateEmployees[i]))
    }
    // create a new object to avoid mutating original data
    return {
        name: manager.name,
        title: manager.title,
        children: children       
    }
}

所有这一切都被称为:

var employees= [
    {name:"Jeff",manager:null,title:"The Pres"},
    {name:"Amy",manager:"Aaron",title:"lll"},
    {name:"Chris",manager:"Aaron",title:"lll"},
    {name:"Dana",manager:"Aaron",title:"lll"},
    {name:"Joe",manager:"Aaron",title:"lll"},
    {name:"Mike",manager:"Aaron",title:"lll"},
    {name:"Aaron",manager:"Jeff",title:"lll"}
]

topManager = employeesForManager(employees, null)[0]
result = giveShape(employees, topManager)  

console.log(result)

Here is the jsfiddle