[英]javascript how to quickly get total count of reports in employee array
我需要確定經理必須在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。 之后,您可以直接引用每個報告,而不必再次循環。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.