我有嵌套数组,我需要按age
排序。意味着如果任何男性年龄最大的女性年龄,那么女性首先。
data = {"people":
[{"male": [
{"name": "Bob" ,"age": "32"},
{"name":"Mike", "age":"22"}
]},
{"male_two": [
{"name": "Rob" ,"age": "12"},
{"name":"Tom", "age":"2"}
]},
{"female": [
{"name":"Jessica", "age": "24"},
{"name":"Ann", "age": "43"}
]}, ...
]}
所以在我的情况下我需要回复:
data = {"people":
[{"female": [
{"name":"Jessica", "age": "24"},
{"name":"Ann", "age": "43"}
]},
{"male": [
{"name": "Bob" ,"age": "32"},
{"name":"Mike", "age":"22"}
]},
{"male_two": [
{"name": "Rob" ,"age": "12"},
{"name":"Tom", "age":"26"}
]}, ...
]}
我可以使用角度$filter
吗?
答案 0 :(得分:0)
您可以编写自己的orderBy
- 函数。
$filter('orderBy')(array, expression, reverse)
https://docs.angularjs.org/api/ng/filter/orderBy
功能:Getter功能。此功能的结果将被排序 使用<,===,>操作
$scope.customOrder = function(gender) {
var highestAge = 0;
for(person in gender) {
if(gender[person].age > highestAge) {
highestAge = gender[person].age;
}
}
return highestAge;
}
您可以在ng-repeat
属性中使用此功能:
ng-repeat="g in people.gender| orderBy:customOrder"
或使用$filter
- 服务(https://docs.angularjs.org/api/ng/service/$filter)。
上面的代码未经测试,但我希望你明白这一点。