所以,我的问题有2个部分,但首先,这是我正在处理的样本数据:
var candidates = [{
"candidateId": 1,
"firstName": "John",
"lastName": "Smith",
"email": "Smith@example.com",
"tasks": [{"taskId":1, "taskState": 3},
{"taskId":2, "taskState": 3},
{"taskId":3, "taskState": 3},
{"taskId":4, "taskState": 1},
{"taskId":5, "taskState": 2}]
},
{
"candidateId": 2,
"firstName": "John",
"lastName": "Doe",
"email": "Doe@example.com",
"tasks": [{"taskId":1, "taskState": 3},
{"taskId":2, "taskState": 3},
{"taskId":3, "taskState": 2},
{"taskId":4, "taskState": 3},
{"taskId":5, "taskState": 3}]
}];
第一部分是:我想排序/命令完成任务编号最高的数字,(比如3表示已完成)。一整天都在努力找到一个没有运气的解决方案。 (尝试通过添加numOfCompletedTasks
属性来扩充对象,然后将其附加到scope / vm以及其他尝试之中)
问题的第二部分是:我开始怀疑这是否是前端的任务,我的意思是,如果我有1000条记录,我必须循环通过,并获得已完成任务的数量,这不是一个后端任务(这里的最佳做法是什么)
答案 0 :(得分:1)
对于第一部分,你看起来像这样吗?
$scope.candidates = [
{
"candidateId": 1,
"firstName": "John",
"lastName": "Smith",
"email": "Smith@example.com",
"tasks": [{"taskId":1, "taskState": 3},
{"taskId":2, "taskState": 3},
{"taskId":3, "taskState": 3},
{"taskId":4, "taskState": 1},
{"taskId":5, "taskState": 2}]
},
{
"candidateId": 2,
"firstName": "John",
"lastName": "Doe",
"email": "Doe@example.com",
"tasks": [{"taskId":1, "taskState": 3},
{"taskId":2, "taskState": 3},
{"taskId":3, "taskState": 2},
{"taskId":4, "taskState": 3},
{"taskId":5, "taskState": 3}]
}];
function customSort(items, field ,value){
var filtered = [];
angular.forEach(items, function(item) {
item.tasks.sort(function(a, b){
return (a[value] < b[value] ? 1 : -1);
});
filtered.push(item);
});
var foo = {"taskState": 3};
filtered.sort(function (a, b) {
return ($filter('filter')(a["tasks"], foo, "strict").length < $filter('filter')(b["tasks"], foo, "strict").length ? 1 : -1);
});
return filtered;
}
$scope.candidates = customSort($scope.candidates, "taskState");
第二: 但我仍然相信在后端做这件事会更明智。