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 ?

1

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

5 Comments

Why Math as the 1st arg to apply()?
.map(/* ... */ .map( /* ... */)).join().split(",")??? Why go through all the trouble for a trivial operation like flattening an array?
I just want to get all baseFare in a single Array to process it
@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-ping?
Yes flatMap is better way to get rid of all join and split i have edited the answer
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))));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.