Javascript 在Angular 6中按对象字段对对象数组进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51194830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort Array of object by object field in Angular 6
提问by Steve
I am getting an array of "product"s from a resolver getting data from a json endpoint.
我从解析器获取一组“产品”,从 json 端点获取数据。
ngOnInit() {
this.products = this._route.snapshot.data.products;
console.log('products: ', this.products);
}
where one of the objects in this array is in the format
此数组中的对象之一的格式为
{
"id": 3645,
"date": "2018-07-05T13:13:37",
"date_gmt": "2018-07-05T13:13:37",
"guid": {
"rendered": ""
},
"modified": "2018-07-05T13:13:37",
"modified_gmt": "2018-07-05T13:13:37",
"slug": "vpwin",
"status": "publish",
"type": "matrix",
"link": "",
"title": {
"rendered": "VPWIN"
},
"content": {
"rendered": "",
"protected": false
},
"featured_media": 0,
"parent": 0,
"template": "",
"better_featured_image": null,
"acf": {
"domain": "SMB",
"ds_rating": "3",
"dt_rating": ""
},
...
},
What I want to do is sort this array by the field title.rendered
我想要做的是按字段对这个数组进行排序 title.rendered
In olden times, in AngularJS, I would simply use an orderBypipe in the template set to this field. Apparently, this is removed in Angular and from doing research it seems the preferred method is to sort the data itself, such as in ngOnInit.
在过去,在 AngularJS 中,我会简单地orderBy在模板中使用一个管道设置为这个字段。显然,这在 Angular 中被删除了,从研究来看,似乎首选的方法是对数据本身进行排序,例如在ngOnInit.
But I can't figure out how to sort productsby producs.title.rendered.
但我无法弄清楚如何排序products的producs.title.rendered。
回答by amrender singh
You can simply use Arrays.sort()
你可以简单地使用 Arrays.sort()
array.sort((a,b) => a.title.rendered.localeCompare(b.title.rendered));
Working Example :
工作示例:
var array = [{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"VPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""},},{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"adfPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""}},{"id":3645,"date":"2018-07-05T13:13:37","date_gmt":"2018-07-05T13:13:37","guid":{"rendered":""},"modified":"2018-07-05T13:13:37","modified_gmt":"2018-07-05T13:13:37","slug":"vpwin","status":"publish","type":"matrix","link":"","title":{"rendered":"bbfPWIN"},"content":{"rendered":"","protected":false},"featured_media":0,"parent":0,"template":"","better_featured_image":null,"acf":{"domain":"SMB","ds_rating":"3","dt_rating":""}}];
array.sort((a,b) => a.title.rendered.localeCompare(b.title.rendered));
console.log(array);
回答by Surjeet Bhadauriya
Try this
尝试这个
products.sort(function (a, b) {
return a.title.rendered - b.title.rendered;
});
OR
或者
You can import lodash/underscore library, it has many build functions available for manipulating, filtering, sorting the array and all.
你可以导入 lodash/underscore 库,它有许多构建函数可用于操作、过滤、排序数组等等。
Using underscore: (below one is just an example)
使用下划线:(以下只是一个例子)
import * as _ from 'underscore';
let sortedArray = _.sortBy(array, 'title');
回答by Luis felipe De jesus Munoz
Not tested but should work
未测试但应该可以工作
products.sort((a,b)=>a.title.rendered > b.title.rendered)

