javascript如何快速获取员工数组中的报表总数

javascript how to quickly get total count of reports in employee array

本文关键字:报表 数组 何快速 获取 javascript      更新时间:2023-09-26

我需要确定经理必须在d3树图可视化中指定正方形大小的报告总数。数据是一个包含209000条记录的csv文件,其格式为以下列标题:Employee、EmployeeID和ManagerID。

我必须找到的数字包括直接和间接报告——既有经理id等于经理员工id的人,也有向较低级别经理报告的人,以及公司阶梯上的所有报告。

var i, theLength, j, thejLength, k, thekLength;
var runningCount = 0;
function getReportCount(csvAll, employeeId, employee) {
  var csvAllModified = csvAll.filter(function(d) {
      return (d.ManagerID == employeeId);
  });
  runningCount+= csvAllModified.length;
    i=0
    theLength = csvAllModified.length;
    for(; i < theLength; i++){ // LOOP THROUGH ONE LEVEL REPORTS
        //GET REPORTS TWO LEVEL BELOW
        csvAllModified2 = csvAll.filter(function(d) {
          return (d.ManagerID == csvAllModified[i].EmployeeID);
        });     
        //managerIds.push(csvAllModified[i].EmployeeID);
        runningCount += csvAllModified2.length;
        j=0;
        thejLength = csvAllModified2.length;
        for(; j < thejLength; j++ ){
            //GET REPORTS THREE LEVELS BELOW
            csvAllModified3 = csvAll.filter(function(d) {
              return (d.ManagerID == csvAllModified2[j].EmployeeID);
            });
            runningCount += csvAllModified3.length;
            k=0;
            thekLength = csvAllModified3.length;
            for(; k < thekLength; k++) {
                //console.log('Employee name 3 levels below: ' +         csvAllModified3[k].Employee); 
                csvAllModified4 = csvAll.filter(function(d) {
                  return (d.ManagerID == csvAllModified3[k].EmployeeID);
                });
                runningCount += csvAllModified4.length;
            }
        }
    }
  return runningCount
}

我的问题是:如何构建上面的代码,使其更高效地运行?目前跑步需要几分钟时间。我必须再添加几个管理层才能得到最终的总数。

您的代码很慢,因为您在大数组上循环了好几次。您希望找到一种不必在原始数组上循环太多次的方法。在这种情况下,我会使用csvAll.map(…(创建一个哈希,您可以直接在中引用报表,这样您就可以使用reports[employeeID]来选择报表。这样,您只需要循环一次就可以创建hashmap,循环一次就能找到初始managerID。A之后,您可以直接引用每个报告,而不必再次循环。