【问题标题】:How to get the count of property which has same value?如何获得具有相同价值的财产的数量?
【发布时间】:2017-02-21 20:31:18
【问题描述】:

我有一个通过 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.
            });

在上面的代码中,如何获取相同价格的门票数量?另外如何获得所有门票中的最高价格?

【问题讨论】:

标签: javascript angular typescript


【解决方案1】:

您必须创建一个哈希表,其中键是您的票价,值是具有该价格的门票数组。例如:

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 有正确的值(数字)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 2021-09-15
    • 1970-01-01
    相关资源
    最近更新 更多