Javascript 如何获取具有相同值的属性的计数?
我有一个通过api检索的json,如下所示Javascript 如何获取具有相同值的属性的计数?,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个通过api检索的json,如下所示 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
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.
});
我正在遍历结果集并创建一个ticket对象,如下所示
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.
});
在上面的代码中,如何获得相同价格的门票数量?另外,如何在所有门票中获得最高价格?您必须创建一个哈希表,其中键是您的门票价格,值是具有该价格的门票数组。例如:
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\u table
的键,在其中找到一个最大值:
var max = 0;
for(var price in price_table){
if(price_table.hasOwnProperty(price)) {
if(price >= max){
max = price;
}
}
}
然后您可以输入最大值以显示最高价格的门票列表:price\u table[max]
上面的示例认为您对
data.ticket.price.original
具有正确的值(数字),仅用于理解:您想按价格对票据进行分组吗?您尝试了什么?这里的答案应该给你一个提示:@floriangosse yes按价格分组