我需要确定经理必须在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(...)创建可以直接引用报表的哈希,以便可以使用report [employeeID]选择报表。这样,您只需要循环一次即可创建哈希图,而只需循环一次即可找到初始的managerID。之后,您可以直接引用每个报表,而不必再次循环。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句