javascript - json对象列表使用角度js中的id列排序
问题描述
在渲染浏览器之前,我需要对我的 json 数据对象进行排序。这是我的 json 格式
$scope.cityData= [
{
id: '520',
city:'col01'
},
{
id: '410',
city:'col02'
},
{
id: '412',
city:'col03'
}]
我试着去做。但我的代码有问题。我控制台记录它。但没有排序
$scope.cityData.sort(function (a, b) { return a.id - b.id });
console.log($scope.cityData);
$scope.newCitySort = $scope.cityData;
我还需要将此排序数据分配给新的范围变量。我该怎么做?
解决方案
如果您不需要对实际的 $scope.cityData 进行排序。您可以只使用orderBy过滤器
在你的 html
<ul ng-repeat="city in cityData | orderBy: 'id' ">
<li>**your list rendering code here**</li>
</ul>
这将使您的收藏品按 id 排序
推荐阅读
- javascript - 如何使幻灯片停止?我可以通过哪些方式改进代码?
- python - 如何使用 python 从在线 pdf 文件中获取数据?
- machine-learning - 有没有什么例子可以教机器理解如何用人工神经网络进行计数?
- docker - 未找到 asp.net 核心 docker linux 程序集
- bitbucket - 如何在 Bitbucket REST API 中验证请求
- laravel - vadliator 不工作并返回 200 OK
- java - 如果元素出现,如何关闭?
- sql - 每次在主表中插入新行时如何在表中插入行?
- php - 如何从互联网访问虚拟主机?
- python - 在 VBA Outlook 中运行 Python 或 PowerShell