let event = [
{
"vendorBidId": 58,
"participantName": "[email protected]",
"bidAmount": 10000,
"productionRate": 10000,
"bidTime": "2021-10-21T14:55:05.957324",
"isYou": false,
"awarded": false
},
{
"vendorBidId": 57,
"participantName": "[email protected]",
"bidAmount": 20000,
"productionRate": 20000,
"bidTime": "2021-10-21T14:50:24.493522",
"isYou": false,
"awarded": true
},
{
"vendorBidId": null,
"participantName": "bro [email protected]",
"bidAmount": 0,
"productionRate": null,
"bidTime": null,
"isYou": false,
"awarded": false
},
{
"vendorBidId": null,
"participantName": "[email protected]",
"bidAmount": 0,
"productionRate": null,
"bidTime": null,
"isYou": true,
"awarded": false
}
]
Here I want to find the minimum bidAmount, but after checking below condition,
- Skip if vendorBidId is null
I tried to do like this.
let minimum = event.reduce(function(prev, curr) {
return prev.bidAmount < curr.bidAmount ? prev : curr;
});
But and tried to add my condition also. But doesn't work well.
I want just the minimum bidAmount (skip for vendorBidId is null) only.
CodePudding user response:
Math.min on a Filter and map
let event = [{ "vendorBidId": 58, "participantName": "[email protected]", "bidAmount": 10000, "productionRate": 10000, "bidTime": "2021-10-21T14:55:05.957324", "isYou": false, "awarded": false }, { "vendorBidId": 57, "participantName": "[email protected]", "bidAmount": 20000, "productionRate": 20000, "bidTime": "2021-10-21T14:50:24.493522", "isYou": false, "awarded": true }, { "vendorBidId": null, "participantName": "bro [email protected]", "bidAmount": 0, "productionRate": null, "bidTime": null, "isYou": false, "awarded": false }, { "vendorBidId": null, "participantName": "[email protected]", "bidAmount": 0, "productionRate": null, "bidTime": null, "isYou": true, "awarded": false } ]
const min = Math.min(...event.filter(evt => evt.vendorBidId).map(({bidAmount}) => bidAmount))
console.log(min)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Using reduce
is a simple approach (if you want the entire object).
If you just need the minimum vakue, you can use Math.min
after mapping the minAmount key values.
let event = [
{
"vendorBidId": 58,
"participantName": "[email protected]",
"bidAmount": 10000,
"productionRate": 10000,
"bidTime": "2021-10-21T14:55:05.957324",
"isYou": false,
"awarded": false
},
{
"vendorBidId": 57,
"participantName": "[email protected]",
"bidAmount": 20000,
"productionRate": 20000,
"bidTime": "2021-10-21T14:50:24.493522",
"isYou": false,
"awarded": true
},
{
"vendorBidId": null,
"participantName": "bro [email protected]",
"bidAmount": 0,
"productionRate": null,
"bidTime": null,
"isYou": false,
"awarded": false
},
{
"vendorBidId": null,
"participantName": "[email protected]",
"bidAmount": 0,
"productionRate": null,
"bidTime": null,
"isYou": true,
"awarded": false
}
]
const min = event.reduce(function(prev, curr) {
return prev.bidAmount < curr.bidAmount && curr.vendorBidId ? prev : curr;
});
console.log(min);
var minValue = Math.min(...event.map(item => item.vendorBidId ? item.bidAmount : 0));
console.log(minValue);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>