简体   繁体   English

如何找到给定员工代码的经理链以及如何找到给定经理代码下的所有员工

[英]how to find managerchain for a given employeecode and how to find all employees under the given managercode

each employee (with given employeecode) works under a manager (respective managercode).每个员工(具有给定的员工代码)在经理(各自的经理代码)下工作。 and if any employee don't have managercode then he is the CEO.如果任何员工没有经理代码,那么他就是 CEO。 I want to write two functions getEmployeesOfManager and getManagerChainOfEmployee in javascript我想用javascript写两个函数getEmployeesOfManager和getManagerChainOfEmployee

var data=[
    {employeeCode: 20, managerCode: 30},
    {employeeCode: 30, managerCode: 40},
    {employeeCode: 40},
    {employeeCode: 50, managerCode: 40},
]
function getEmployeesOfManager(managerCode) {
    // write your code here
    // here we have to store all employees that comes under the manager of the given managercode in an array
}
function getManagerChainOfEmployee(employeeCode) {
    // write your code here
    // here we have to store all the managers which are above the given employeecode like for employeecode - 20 it will be [30,40] as 20 works under 30 and 30 works under 40.
}

Question 1 is simple问题1很简单
You just need to determine the managerCode of the data by ordinary traversal只需要通过普通遍历确定数据的managerCode
Problem 2 is a little more complicated问题2有点复杂
First you need to find the employee with your current employeeCode首先,您需要使用您当前的employeeCode 找到该员工
Determine if this employee exists确定该员工是否存在
If it exists如果存在
then determine whether this employee is a normal employee然后判断这个员工是否是普通员工
If it is如果是
then iterate through the array and find the employee's supervisor然后遍历数组并找到员工的主管
If the employee's supervisor exists如果员工的主管存在
then record this supervisor然后记录这个主管
and then use this superior as the target然后以这个上级为目标
then recursively loop然后递归循环
Also, you can do a memorized search此外,您可以进行记忆搜索
Optimize performance by reducing unnecessary searches通过减少不必要的搜索来优化性能

var data=[
    {employeeCode: 20, managerCode: 30},
    {employeeCode: 30, managerCode: 40},
    {employeeCode: 40},
    {employeeCode: 50, managerCode: 40},
]
const visitedData = new Set()
function getEmployeesOfManager(managerCode) {
    let employees = []
    for (const item of data) {
        if (item.managerCode === managerCode) {
            employees.push(item)
        }
    }
    return employees
}
function getManagerChainOfEmployee(employeeCode) {
    const target = data.find(i => i.employeeCode === employeeCode)
        
    let chain = []
    if (target.managerCode) {
        chain = [target.managerCode]
        visitedData.add(`${target.employeeCode}-${target.managerCode}`)
        for (const item of data) {
            const id = `${item.employeeCode}-${item.managerCode}`
            if (item.employeeCode === target.managerCode && !visitedData.has(id)) {
                visitedData.add(id)
                chain.push(item.managerCode)
                chain = chain.concat(getManagerChainOfEmployee(item.managerCode))
            }
        }
    }

    return chain
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM