I have array with objects
const data = [
{ "bankNumber": 1, "calculatedFee": 0, "feeNumber": 4, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 147, "feeNumber": 6, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "1" },
{ "bankNumber": 1, "calculatedFee": 200, "feeNumber": 10, "group": "2"},
{ "bankNumber": 1, "calculatedFee": 20, "feeNumber": 10, "group": "2" },
{ "bankNumber": 2, "calculatedFee": 10, "feeNumber": 10, "group": "3" },
{ "bankNumber": 2, "calculatedFee": 100, "feeNumber": 10, "group": "3" },
{ "bankNumber": 3, "calculatedFee": 100, "feeNumber": 10, "group": "2" },
]
I would like to filter this data to get the smallest value calculatedFee with one group and bankNumber. For example calculatedFee = 0 is the smallest for group 1 and bankNumber 1, calculatedFee = 10 is the smallest for group 3 and bankNumber 2,. However, I have no idea what function to create in JavaScript to get such results
2
Answers
Use
Math.min()
along withfilter()
andmap()
You can use
Array#reduce
with an object to store the minimum fee for each combination ofbankNumber
andgroup
.