2017-07-06 5 views
1

これは私のcomponents.tsです。 カウントを配列に並べ替えるfuctionを書いて、htmlで並べ替えられた配列データを使ってグラフを作成します。配列を使用してデータをソートする

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', 
    }, 
    ... 
    } 
] 
+0

回答に加えて。私が推奨する[Lodash](https://lodash.com/)と呼ばれるこのきちんとしたライブラリがあります。 – indexoutofbounds

答えて

1

あなたは、参考のためsort機能

this.data.sort(function(a,b){ 
    return a.count - b.count; 
}); 

を使用することができます - tweaking- plunker

0

についてはsort

あなたがデータの順序を表示したい場所をごdashboard.htmlファイルでこれを実装しますカウント別

<tr *ngFor="#friend in friends| orderBy : ['count']""> 
     <td>{{friend.name}}</td> 
     <td>{{friend.phone}}</td> 
     <td>{{friend.count}}</td> 
</tr> 
+0

ちょっと考えました.OPは、データがチャートのために必要であり、 'orderBy'が利用できないので、JS内の関数でデータをソートする必要があります。 – nikhil

2
this.data.sort(function (a, b) { 
    return a.count - b.count; 
}); 

出典:https://developer.mozilla.org/hu/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

それともES6ショートカット:この方法のように

data.sort((a, b) => a.count - b.count); 

は1行であなたは{}もリターンを必要といけないし、また許可を必要といけないので矢印機能。

+2

またはちょうどES6ショートカット: data.sort((a、b)=> a.count - b.count); あなたは{}を必要としないので、1行では戻り値は必要ありません。矢印も使えます –

関連する問題