如何获得最高和最低的数字

时间:2016-08-15 13:10:42

标签: javascript jquery json ajax

所以我有一组看起来像这样的代码

  for (var i = 0; i < secname.length; i++) {
    description = secname[i].description
    price = secname[i].price_breakdown.display_charges.price
    debugger;
  }

目前我的问题是每个对象传回3-4个价格。

理想情况下,我想要的是具有描述的最低和最高价格。在进入下一个描述之前,它目前会返回(使用相同的描述)4个价格。

我还想把它放到我的桌子上。我有前置代码,我只是不确定在哪里放置它。

所以tl; dr我有一张桌子,描述名称是唯一的,有4个价格。我希望我的桌子附上最高价格和最低价格。

萨姆

修改

继承代码

                            "CIRCLE": [
                          {
                            "price_secname": "P1",
                            "price_breakdown": {

                              "price_secname": "P1",
                              "vat": 0,
                              "distance_charge": 0,
                              "display_charges": {
                                "price": 35,
                                "sum_fees": "5.25",
                                "formatted_total_price": "&Acirc;&pound;40.25"
                              },

                              "legacy_price": 35
                            },

                            "description": "Circle",
                            "ticket_desc": "Full Price Ticket",

                          },
                          {
                            "price_secname": "P2",
                            "price_breakdown": {

                              "price_secname": "P2",
                              "vat": 0,
                              "distance_charge": 0,
                              "display_charges": {
                                "price": 50,
                                "sum_fees": "5.25",
                                "formatted_total_price": "&Acirc;&pound;50.25"
                              },

                              "legacy_price": 35
                            },

                            "description": "Circle",
                            "ticket_desc": "Full Price Ticket",

                          },
                          {
                            "price_secname": "P3",
                            "price_breakdown": {

                              "price_secname": "P3",
                              "vat": 0,
                              "distance_charge": 0,
                              "display_charges": {
                                "price": 40,
                                "sum_fees": "5.25",
                                "formatted_total_price": "&Acirc;&pound;45.25"
                              },

                              "legacy_price": 40
                            },

                            "description": "Circle",
                            "ticket_desc": "Full Price Ticket",

                          }
                                ]

1 个答案:

答案 0 :(得分:2)

刚刚建立Rejesh答案,基本上循环遍历每个对象并拉出最大,最小和描述。

for (var i = 0; i < circle.length; i++) {
    var description = circle[i].description
    var price = circle.map(function(x){ return x.price_breakdown.display_charges.price; }); 
    var max = Math.max.apply(null, price); 
    var min = Math.min.apply(Math, price.filter(Number));
    console.log(description)
    console.log("Max: " + max);
    console.log("Min: " + min);    
}

片段

http://jsbin.com/votoheceha/4/edit?html,js,console,output