{
"35": "testing",
"46": "tst123",
"51": "alpha_circular",
"56": "test4",
"60": "alpha_price",
"64": "test_attached",
"70": "test789",
"71": "new cir",
"73": "valid"
}
我需要根据密钥按降序对上述数据进行排序。我怎样才能实现它?
此外,我已在 $ index "列表中使用ng-repeat="(id,value)"
。我可以在这种情况下应用过滤器吗?
答案 0 :(得分:0)
我检查过,发现orderBy仅适用于数组。所以你必须改变JSON。
答案 1 :(得分:0)
不是Angular
解决方案。只需JS
解决方案即可使用密钥对Object
进行排序。
var obj = {
"35": "testing",
"46": "tst123",
"51": "alpha_circular",
"56": "test4",
"60": "alpha_price",
"64": "test_attached",
"70": "test789",
"71": "new cir",
"73": "valid"
};
function comparator(a, b){
return b-a;
}
Object.keys(obj).sort(comparator).forEach(function(key, i) {
console.log(key, obj[key]);
});
答案 2 :(得分:0)
试试这个
var app = angular.module('app',[])
app.controller('ctrl',function($scope){
$scope.data = {
"35": "testing",
"46": "tst123",
"51": "alpha_circular",
"56": "test4",
"60": "alpha_price",
"64": "test_attached",
"70": "test789",
"71": "new cir",
"73": "valid"
};
$scope.array = [];
angular.forEach($scope.data,function(d,i){
$scope.array.push({id:i,value:d});
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div ng-repeat="(key,value) in array | orderBy:'-id'">
{{value.value}}
</div>
</div>
答案 3 :(得分:0)
尝试以下解决方案
在你的HTML中
function FooController($scope) {
$scope.keys = function(){
var keysList = [];
angular.forEach($scope.items, function(value, key) {
keysList.push({"key": parseInt(key), "value": value});
});
return keysList
}
$scope.items = {
"35": "testing",
"46": "tst123",
"51": "alpha_circular",
"56": "test4",
"60": "alpha_price",
"64": "test_attached",
"70": "test789",
"71": "new cir",
"73": "valid"
在您的控制器中
73 :: valid
71 :: new cir
70 :: test789
64 :: test_attached
60 :: alpha_price
56 :: test4
51 :: alpha_circular
46 :: tst123
35 :: testing
} }
输出应为
{{1}}
希望这对你有帮助。
答案 4 :(得分:0)
我按照下面的解决方案希望它是正确的选择:
首先,我使用了反转键, $ scope.keys = Object.keys($ scope.obj).reverse();
然后在我看来
<div ng-repeat="key in keys">
{{obj[key]}}
</div>
让我知道它是否正确!