用于构建Orgchart的Jquery递归对象迭代

时间:2014-10-24 09:12:55

标签: jquery object recursion orgchart

我有一个代表国家/地区的对象列表。

每个国家都有一群员工。 每个员工对象都拥有自己的属性以及对其经理的引用。

员工可以让不经验的经理或经理坐在不同的国家。

我正在尝试使用员工与经理之间的父子关系重建新对象。

var Markets = {
"CH":{
        "Resources": {
            "user1": {"First name": "John", "Last name": "Blue", "Job Title": "Job 1", "Email": "john@email", "Manager": ""},               
            "user2": {"First name": "Bob", "Last name": "Black", "Job Title": "Job 2", "Email": "bob@email", "Manager": "user1"},
            "user3": {"First name": "Joe", "Last name": "Brown", "Job Title": "Job 3", "Email": "joe@email", "Manager": "user1"},
            "user4": {"First name": "Jack", "Last name": "White", "Job Title": "Job 4", "Email": "jack@email", "Manager": "user2"}
            "user5": {"First name": "Jess", "Last name": "Red", "Job Title": "Job 5", "Email": "jess@email", "Manager": ""}
        }
    }

因此我的新对象如下:

user 1
    user 2
    user 3
        user 4
user 5

我已经提出了以下功能,但我很难将其递归,以确保将二级管理员重新分配到第一级。

function countryOrgChart(market) {
    var orgChartData = {};
    for (var resource in window['Markets'][market]['Resources']) {
        orgChartData[resource] = [];
    }
    orgCharDataTmp = orgChartData;
    for (var resource in orgCharDataTmp {
        var manager = window['Markets'][market]['Resources'][resource]['Manager'];
        if (manager in orgChartData) { 
            orgChartData[manager].push(resource); 
            delete orgChartData[resource]; 
        }
    }
    return(orgChartData);

}

1 个答案:

答案 0 :(得分:0)

创建它不需要递归,只需在创建后导航它。

  • 创建所有用户的查找
  • 根据经理用户ID
  • 将员工附加到经理
  • 递归显示

http://jsfiddle.net/TrueBlueAussie/orbp4w3n/5/

// Build a dictionary of all employees
var emps = [];

// Iterate the markets
for (var marketkey in Markets) {
    var market = Markets[marketkey];

    // Iterate the departments in a market
    for (var departmentkey in market) {
        var department = market[departmentkey];

        // Iterate the employess in a department
        for (var employeekey in department) {

            // Store the required employee details in an array
            var employee = department[employeekey];
            console.log(employee);
            emps.push({
                user: employeekey,
                manager: employee.Manager,
                employees: []
            });
        }
    }
}

// Also build a list of all employees with no manager
var directors = [];

// Connect the employee to their manager
for (var i = 0; i < emps.length; i++) {
    var employee = emps[i];

    // if the employee has a manager
    if (employee.manager) {

        // Find the manager object
        var mgr = $.grep(emps, function (e) {
            return e.user == employee.manager;
        });
        if (mgr.length) {
            var manager = mgr[0];

            // if found add this employee to their list of employees
            manager.employees.push(employee);

            // get rid of the reference
            delete employee.manager;
        }
    } else {
        directors.push(employee);
    }
}

注意: 我没有减少代码,因为我的目标是可读性。