0

this is my array:

[{
            "AC": "true",
            "doj": "2020-11-20T00:00:00+05:30",
            "fareDetails": [
                {
                    "baseFare": "1033.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1085.00"
                },
                {
                    "baseFare": "1086.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1140.00"
                },
                {
                    "baseFare": "1190.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1250.00"
                },
                {
                    "baseFare": "1243.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1305.00"
                }
            ],
            "fares": [
                "1250.00",
                "1305.00",
                "1140.00",
                "1085.00"
            ],
        },..........],

I need to show the lowest "baseFare" among the given array, as there are four base fare i need show only the lowest and it should be in numbers like 1033 without decimals

So far i tried this , but it gives error

 Math.round(Math.min(...item.fareDetails.baseFare)

How to achive this ?

samanthas
  • 79
  • 1
  • 7
  • 3
    [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865) -> then get `Math.min` from that. – VLAZ Nov 11 '20 at 15:49

2 Answers2

1

you can use the combination of Math.min and yourArry.map

like this

Math.min.apply( Math, t[0].fareDetails.map(x=>x.baseFare))

var t =[{
            "AC": "true",
            "doj": "2020-11-20T00:00:00+05:30",
            "fareDetails": [
                {
                    "baseFare": "1033.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1085.00"
                },
                {
                    "baseFare": "1086.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1140.00"
                },
                {
                    "baseFare": "1190.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1250.00"
                },
                {
                    "baseFare": "1243.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1305.00"
                }
            ],
            "fares": [
                "1250.00",
                "1305.00",
                "1140.00",
                "1085.00"
            ],
        },{
            "AC": "true",
            "doj": "2020-11-20T00:00:00+05:30",
            "fareDetails": [
                {
                    "baseFare": "12.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1085.00"
                },
                {
                    "baseFare": "444.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1140.00"
                },
                {
                    "baseFare": "22.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1250.00"
                },
                {
                    "baseFare": "111.00",
                    "srtFee": "0.0",
                    "tollFee": "0.0",
                    "totalFare": "1305.00"
                }
            ],
            "fares": [
                "1250.00",
                "1305.00",
                "1140.00",
                "1085.00"
            ],
        }]
var lowest =Math.min.apply(Math,t.flatMap(x=> x.fareDetails.map(x=>x.baseFare)))
console.log(lowest)
Zulqarnain Jalil
  • 1,679
  • 16
  • 26
  • Why Math as the 1st arg to apply()? – Torbjörn Stabo Nov 11 '20 at 16:02
  • `.map(/* ... */ .map( /* ... */)).join().split(",")`??? Why go through all the trouble for a trivial operation like flattening an array? – VLAZ Nov 11 '20 at 16:11
  • I just want to get all ``baseFare`` in a single Array to process it – Zulqarnain Jalil Nov 11 '20 at 16:14
  • @ZulqarnainJalil yes, I'm aware of that. I wonder why convert to a string and split it back into another array instead of just [`flatMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap)-ping? – VLAZ Nov 11 '20 at 16:17
  • Yes ``flatMap`` is better way to get rid of all ``join`` and ``split`` i have edited the answer – Zulqarnain Jalil Nov 11 '20 at 16:21
0

You need to extract baseFare values from the array item and get the min value among them as follows.

Math.round(Math.min(...item.map(({ fareDetails: { baseFare } }) => Number(baseFare))));
Derek Wang
  • 10,098
  • 4
  • 18
  • 39