Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -26,7 +26,7 @@ function top_rank(n) {
|
|||
}
|
||||
}
|
||||
|
||||
// group by dept, and sort by balary
|
||||
// group by dept, and sort by salary
|
||||
function group_by_dept(data) {
|
||||
var by_dept = {};
|
||||
for (var idx in data) {
|
||||
31
Task/Top-rank-per-group/JavaScript/top-rank-per-group-2.js
Normal file
31
Task/Top-rank-per-group/JavaScript/top-rank-per-group-2.js
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
var collectDept = function (arrOfObj) {
|
||||
var collect = arrOfObj.reduce(function (rtnObj, obj) {
|
||||
if (rtnObj[obj.dept] === undefined) {
|
||||
rtnObj[obj.dept] = [];
|
||||
}
|
||||
rtnObj[obj.dept].push(obj);
|
||||
return rtnObj;
|
||||
}, {});
|
||||
|
||||
return Object.keys(collect).map(function (key) {
|
||||
return collect[key];
|
||||
});
|
||||
};
|
||||
|
||||
var sortSalary = function (arrOfSalaryArrs) {
|
||||
return arrOfSalaryArrs.map(function (item) {
|
||||
return item.sort(function (a, b) {
|
||||
if (a.salary > b.salary) { return -1; }
|
||||
if (a.salary < b.salary) { return 1; }
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var getNTopSalariesByDept = function (n, data) {
|
||||
if (n < 0) { return; }
|
||||
|
||||
return sortSalary(collectDept(data)).map(function (list) {
|
||||
return list.slice(0,n);
|
||||
});
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue