3

我需要一个 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:[]
        }]
    }]
};

但它超出了我的范围。

4

2 回答 2

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

提琴手

于 2012-09-05T17:56:07.667 回答
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)

这是jsfiddle

于 2012-09-05T21:17:48.273 回答