如何使用递归创建一个计数器来获取 javascript 中 manager_id 下的员工总数(直接或间接)?

How to create a counter using recursion to get the total employees(direct or indirect) under a manager_id in javascript?

我有一个 excel 文件,其中包含这样的数据

Name WorkerId ManagerId
Tom 179 180
Liz 150 179
Ricki 120 179
sona 113 150
Preet 558 150
mina 89 558
Yukti 45 120

我想要一个 CountEmployee(manager_id) 函数,它将 return 他手下的所有员工。例如:

CountEmployee(179) = 6 , CountEmployee(150) = 3

我正在使用 papa 解析库将此 excel 解析为对象,并编写了一个递归函数来获取员工总数。

parseCsv to parse the csv

 function parseCsv() {
  return new Promise((resolve, reject) => {
    Papa.parse("Employees.csv", {
      download: true,
      complete: function (results) {
        return resolve(results);
      },
    });
  });
}

CountDirectEmployees to get the Direct employees of a manager Id

 async function CountDirectEmployee(data) {
      var countDirect1 = 0
       const results = await parseCsv();
        results.data.map((row, index) => {
                        if (row[2] == data) {
                           countDirect1 = countDirect1 +  1
                        }
                    })
     return countDirect1;
    }

最后,

The CountEmployee should return the final count

 async function CountEmployee(data,count) {
         const results = await parseCsv();
         CountDirectEmployee(data).then(
         function(count1){

         results.data.forEach((row, index) => {
                         if (row.ManagerId == data) {
                     count1 = count+count1
               CountEmployee(row[1],count1)
              }
         })
        } 
        )
       return count
   }

我知道我的逻辑在某处对于 CountEmployee 函数是错误的。无法找出问题。

稍微更改代码将允许我们递归调用 CountEmployee。我们会为每次调用传入 rows 变量(以保存重复读取),然后添加每个员工的直接工人数以获得每个经理的总数。

getDirectEmployees() 函数使用简单的 Array.filter() 来 return 直接为每位经理工作的员工。

const employees = [ 
    [ 'Tom', 179, 180 ],
    [ 'Liz', 150, 179 ],
    [ 'Ricki', 120, 179 ],
    [ 'Sona', 113, 150 ],
    [ 'Preet', 558, 150 ],
    [ 'Mina', 89, 558 ],
    [ 'Yukti', 45, 120 ]
];
    
// Simulate what papa parse returns...
function parseCSV() {
    return Promise.resolve(employees); 
}

function displayRow(...row) {
    console.log(...row.map(f => (f + '').padEnd(15)))
}

async function testCounts() {
    console.log('Direct and total counts for each worker:\n');

    let rows = await parseCSV();
    displayRow('Name', 'Id', 'Direct', 'Total')
    for(let [name, workerId, managerId] of rows) {
        let directEmployees = CountDirectEmployees(workerId, rows);
        let totalEmployees = CountEmployee(workerId, rows);
        displayRow(name, workerId, directEmployees, totalEmployees)
    }
}

function getDirectEmployees(id, rows) {
    return rows.filter(([name, workerId, managerId]) => managerId === id);
}

function CountDirectEmployees(managerId, rows) {
    return getDirectEmployees(managerId, rows).length;
}
 
function CountEmployee(managerId, rows) {
    // Count direct employees, then count employees of each of these
    let directEmployees = getDirectEmployees(managerId, rows);
    let count = directEmployees.length;
    for(let [name, workerId] of directEmployees) {
        count += CountEmployee(workerId, rows);
    }
    return count;
}
 
testCounts()
 
.as-console-wrapper { max-height: 100% !important; top: 0; }