2017-02-21 94 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