2017-02-21 96 views
0

我有通過API檢索如下一個JSON,如何獲得具有相同價值的財產計數?

http://www.jsoneditoronline.org/?id=3d6078cbb11e7e5b3989320ff1cc00c1

我循環通過結果集並創建票證對象如下,

data.ticket.seating.forEach((seat: any) => { 
       this.listings.push({ section: seat.section, selling: data.price.selling, amount: data.ticket.amount, type: data.ticket.type, row: seat.row }); 
       this.barChartLabels.push(data.price.selling); 
       this.barChartData[0].data.push() // how to push the count of tickets which has the same price. 
      }); 

在上面的代碼,如何獲得具有相同價格的門票數量?還有如何獲得所有門票中的最高價格?

+0

僅供瞭解:您想按價格對門票進行分組嗎? – floriangosse

+0

你有什麼嘗試?這裏的答案應該給你一個提示:[*如何保存兩個或更多數組中存在的項目並返回該數組?](http://stackoverflow.com/questions/42336471/how-to-preserve-items -thats-present-in-two-or-more-arrays-and-return-an-array-o) – RobG

+0

@floriangosse是按價格對門票進行分組 – user2280016

回答

0

您將不得不製作一個哈希表,其中的關鍵字是您的票價和值是具有該價格的票的數組。例如:

var price_table = {}; 
entries.forEach((data: any) => { 
    if(!price_table.hasOwnProperty(data.ticket.price.original)) 
     price_table[data.ticket.price.original] = []; 
    price_table[data.ticket.price.original].push(data); 
}); 

運行的代碼後,你必須通過price_table鍵,並找到其中的一個最大值:

var max = 0; 
for(var price in price_table){ 
    if(price_table.hasOwnProperty(price)) { 
     if(price >= max){ 
      max = price; 
     } 
    } 
} 

然後你就可以將你最大的值,顯示價格最高的門票清單:price_table[max]

上面的例子認爲你有合適的值(數字)data.ticket.price.original