angular ng-重复(键,值)|按值排序
angular ng-repeat (key, value) | order by value
是否可以这样排序数组:
ng-repeat="(key, value) in sets | orderBy: value['ordering']"
这是我的数组:
[{
"item 1": {
"type": "text",
"value": "1",
"ordering": 1
},
"item 2": {
"type": "text",
"value": "4",
"ordering": 3
},
"item 3": {
"type": "text",
"value": "8",
"ordering": 2
}
}]
它应该输出:Item 1, Item 3, Item 2
编辑:我也需要钥匙
我不需要 key
,你可能不想尝试这个:
您误解了您拥有的数组。
您的数组由一个大对象组成,您无法在 javascript 中对对象进行排序。修复结构,使您拥有多个对象的数组。
[{
"item": 1,
"type": "text",
"value": "1",
"ordering": 1
}, {
"item": 2,
"type": "text",
"value": "4",
"ordering": 3
}, {
"item": 3,
"type": "text",
"value": "8",
"ordering": 2
}]
然后就可以使用普通的数组转发器了
ng-repeat="set in sets | orderBy: 'ordering'"
是否可以这样排序数组:
ng-repeat="(key, value) in sets | orderBy: value['ordering']"
这是我的数组:
[{
"item 1": {
"type": "text",
"value": "1",
"ordering": 1
},
"item 2": {
"type": "text",
"value": "4",
"ordering": 3
},
"item 3": {
"type": "text",
"value": "8",
"ordering": 2
}
}]
它应该输出:Item 1, Item 3, Item 2
编辑:我也需要钥匙
我不需要 key
,你可能不想尝试这个:
您误解了您拥有的数组。
您的数组由一个大对象组成,您无法在 javascript 中对对象进行排序。修复结构,使您拥有多个对象的数组。
[{
"item": 1,
"type": "text",
"value": "1",
"ordering": 1
}, {
"item": 2,
"type": "text",
"value": "4",
"ordering": 3
}, {
"item": 3,
"type": "text",
"value": "8",
"ordering": 2
}]
然后就可以使用普通的数组转发器了
ng-repeat="set in sets | orderBy: 'ordering'"