gpt4 book ai didi

javascript - 排列 $scope 内对象的顺序

转载 作者:行者123 更新时间:2023-12-03 08:11:51 25 4
gpt4 key购买 nike

我想知道是否可以更改范围内对象的顺序。我有一个 $scope.movi​​es 现在该范围是按 ID 排序的。因此,当我通过控制台记录 $scope.movi​​es 时,它看起来像这样,

{"id":1,"title":"The Town","release_date":"2010-10-21","image":"/zX4fKmDXKGt4hlzhAJirdlRKFgO.jpg","user_id":null,"created_at":"2015-12-04T19:57:58.449Z","updated_at":"2015-12-04T19:57:58.449Z","movie_id":"23168","imdb_rating":"7.6"},
{"id":2,"title":"Interstellar","release_date":"2014-11-06","image":"/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg","user_id":null,"created_at":"2015-12-04T19:58:16.600Z","updated_at":"2015-12-04T19:58:16.600Z","movie_id":"157336","imdb_rating":"8.7"},
{"id":3,"title":"Django Unchained","release_date":"2013-01-16","image":"/5WJnxuw41sddupf8cwOxYftuvJG.jpg","user_id":null,"created_at":"2015-12-04T20:00:22.411Z","updated_at":"2015-12-04T20:00:22.411Z","movie_id":"68718","imdb_rating":"8.5"},
{"id":4,"title":"Moonrise Kingdom","release_date":"2012-05-30","image":"/uw88gWDC0W7AAEhMeQmtdXFV7yR.jpg","user_id":null,"created_at":"2015-12-04T20:00:41.054Z","updated_at":"2015-12-04T20:00:41.054Z","movie_id":"83666","imdb_rating":"7.8"},
{"id":5,"title":"Gone Girl","release_date":"2014-10-01","image":"/gdiLTof3rbPDAmPaCf4g6op46bj.jpg","user_id":null,"created_at":"2015-12-05T09:05:08.424Z","updated_at":"2015-12-05T09:05:08.424Z","movie_id":"210577","imdb_rating":"8.2"}

但我想按发布日期排列数据,所以它看起来像这样,

{"id":1,"title":"The Town","release_date":"2010-10-21","image":"/zX4fKmDXKGt4hlzhAJirdlRKFgO.jpg","user_id":null,"created_at":"2015-12-04T19:57:58.449Z","updated_at":"2015-12-04T19:57:58.449Z","movie_id":"23168","imdb_rating":"7.6"},
{"id":4,"title":"Moonrise Kingdom","release_date":"2012-05-30","image":"/uw88gWDC0W7AAEhMeQmtdXFV7yR.jpg","user_id":null,"created_at":"2015-12-04T20:00:41.054Z","updated_at":"2015-12-04T20:00:41.054Z","movie_id":"83666","imdb_rating":"7.8"},
{"id":3,"title":"Django Unchained","release_date":"2013-01-16","image":"/5WJnxuw41sddupf8cwOxYftuvJG.jpg","user_id":null,"created_at":"2015-12-04T20:00:22.411Z","updated_at":"2015-12-04T20:00:22.411Z","movie_id":"68718","imdb_rating":"8.5"},
{"id":5,"title":"Gone Girl","release_date":"2014-10-01","image":"/gdiLTof3rbPDAmPaCf4g6op46bj.jpg","user_id":null,"created_at":"2015-12-05T09:05:08.424Z","updated_at":"2015-12-05T09:05:08.424Z","movie_id":"210577","imdb_rating":"8.2"}
{"id":2,"title":"Interstellar","release_date":"2014-11-06","image":"/nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg","user_id":null,"created_at":"2015-12-04T19:58:16.600Z","updated_at":"2015-12-04T19:58:16.600Z","movie_id":"157336","imdb_rating":"8.7"},

那么是否可以使用 javascript 更改 $scope 内对象的顺序?

我在我的 ng-repeat 中使用了 orderByrelease_date。但对于我的 html 结构,我必须将电影放入行中。使用这个,

$scope.toRows = function(arr, total){
var i = 0, rows = [];
while(i < arr.length){
i % total == 0 && rows.push([]);
rows[rows.length-1].push(arr[i++])
}
return rows
};
$scope.moviesRows = $scope.toRows($scope.movies, 6);

然后 orderBy 不再正常工作,因为它将 $scope.movi​​es 的第一项放入行中,然后执行 orderBy 位于行中的对象上。因此,它首先将所有对象按 ID 的顺序放置在行中,然后按 release_date 对行中的对象进行排序。

* 编辑*

看来 JSON 输出的顺序与 Angular 如何放置对象无关。

array.sort(function(a,b){
return new Date(b.release_date) - new Date(a.release_date);
});

按对象中以及控制台中的发布日期进行排序。但 Angular 仍然通过 ID 来放入对象。

最佳答案

您可以使用 orderBy 过滤器来实现您想要的。看看:https://docs.angularjs.org/api/ng/filter/orderBy

无论如何,如果您进行了大量的集合操作,那么您最好看看几个不错的库之一,例如 UnderscoreJS、Lodash 等。

我使用Lodash对于它,但这只是一种偏好。

根据进一步要求:

在将数组分成 block 之前,您必须对数组进行排序,因此在 Controller 上注入(inject) $filter 并执行以下操作:

var orderBy = $filter('orderBy');
$scope.orderedMovies = orderBy($scope.movies, "release_date", false);
$scope.moviesRows = $scope.toRows($scope.orderedMovies, 6);

关于javascript - 排列 $scope 内对象的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34104220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com