Json 使用数组对数据进行排序
这是我的组件。 我想写一个函数,按计数对数组进行排序,我将使用html中排序的数组数据创建一个图表Json 使用数组对数据进行排序,json,angular,sorting,typescript,ng2-charts,Json,Angular,Sorting,Typescript,Ng2 Charts,这是我的组件。 我想写一个函数,按计数对数组进行排序,我将使用html中排序的数组数据创建一个图表 import { Component } from '@angular/core'; @Component({ selector: 'dashboard', styleUrls: ['./dashboard.scss'], templateUrl: './dashboard.html', }) export class Dashboard { private data = [];
import { Component } from '@angular/core';
@Component({
selector: 'dashboard',
styleUrls: ['./dashboard.scss'],
templateUrl: './dashboard.html',
})
export class Dashboard {
private data = [];
constructor() {
this.data = [{
'maxTime': 30041,
'minTime': 6453,
'avgTime': 18949,
'count': 4,
'requestRouteTemplate': 'api/GetUserPostponesCountReport',
'requestMethod': 'POST',
},
...
}
]
您可以使用
排序
功能
this.data.sort(function(a,b){
return a.count - b.count;
});
供参考-
对于调整-在dashboard.html文件中实现此功能,您希望在其中按计数显示数据顺序 这样说:
<tr *ngFor="#friend in friends| orderBy : ['count']"">
<td>{{friend.name}}</td>
<td>{{friend.phone}}</td>
<td>{{friend.count}}</td>
</tr>
资料来源:
或者只是ES6的快捷方式:
data.sort((a, b) => a.count - b.count );
因为在一行中,您不需要{}
也不需要返回,还允许使用箭头功能。或者只是一个ES6快捷方式:data.sort((a,b)=>a.count-b.count);因为在一行中,您不需要{}也不需要返回,还允许使用箭头函数只是一个想法-OP希望数据在JS中的函数中排序,因为图表需要数据,orderBy
可能不可用。这是对答案的补充。有一个整洁的图书馆,我推荐它。使用lodash,您可以简单地data=u.sortBy(数据,'count')代码>
data.sort((a, b) => a.count - b.count );