使用 AngularJS 按数字键对对象进行排序

Sorting Object by keys numerically with AngularJS

好的,所以我正在尝试将我的 json 数据整理到一个表中:

{
    "0": {
        "1": {
            "airdate": "2011-12-04", 
            "file_size": 368279154, 
            "location": "filepath", 
            "name": "episodename", 
            "quality": "Unknown", 
            "release_name": "", 
            "status": "Downloaded", 
            "subtitles": ""
        }
    }, 
    "1": {
        "1": {
            "airdate": "2011-12-04", 
            "file_size": 368279154, 
            "location": "filepath", 
            "name": "episodename1", 
            "quality": "Unknown", 
            "release_name": "", 
            "status": "Downloaded", 
            "subtitles": ""
        }, 
        "2": {
            "airdate": "2011-12-04", 
            "file_size": 368279154, 
            "location": "filepath", 
            "name": "episodename2", 
            "quality": "Unknown", 
            "release_name": "", 
            "status": "Downloaded", 
            "subtitles": ""
        }, 
        "14": {
            "airdate": "2011-12-04", 
            "file_size": 368279154, 
            "location": "filepath", 
            "name": "episodename14", 
            "quality": "Unknown", 
            "release_name": "", 
            "status": "Downloaded", 
            "subtitles": ""
        }
    }    

问题是我的 angular ng-repeat 按字母顺序列出了它,例如 1、14、2...我似乎无法使用此对象获取密钥并将其设置为对象的 ID 参数,但我认为这将是最佳选择。任何的意见都将会有帮助。我想指出我不能改变服务器端 api.

如果某些集合的顺序很重要,您应该考虑改用数组。

如果你坚持使用对象,方法是创建一个数组来保存对象的键用于排序目的

原型

scope.obj = {"1":{}, "2":{}, "14":{}};
scope.objKeys = ['1', '2', '14'];

<div ng-repeat="key in objKeys">
    {{obj[key]}}
</div>