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

时间:2022-12-12 13:12:28

So i have this set of code which looks like this

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

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

What my issue is at the moment is that this passes back 3-4 prices per object.

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

What ideally i'm wanting is the lowest and the highest price from these with the description. It currently passes back (with the same description) 4 prices before going into the next description.

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

I would also like to put it into the table i have. i have the prepend code i'm just unsure where to place it.

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

So tl;dr I have a table, The description name is unique and has 4 prices. I want the highest price and lowest price appended to my table.

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

Sam

山姆

Edit

编辑

Heres the code

继承人的代码

                            "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 个解决方案

#1


2  

Just building off Rejesh answer, essentially looping over each object and pulling out the max, min and description.

只是建立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);    
}

Snippet

片段

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

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

#1


2  

Just building off Rejesh answer, essentially looping over each object and pulling out the max, min and description.

只是建立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);    
}

Snippet

片段

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

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