Reputation: 8589
I have an objected named slip
, this object contains a property active
which the attr is '1'
, when the slip
object is not selected the property active
has an attr '0'
.
I have a function that I need to apply only when the property active
has the attr '0'
. I want to know which is the best Lodash method to use here in this case
var applyStraightWin = function(slip, amount) {
var moneyLine = _.result(_.find(slip.lines, {isSelected: '1'}), 'moneyLine');
_.filter(slip.active, function(pickActive) {
if (pickActive == '1') {
_.assign(slip, {
win: amount,
risk: RiskWinCalculations.calculateStraightBet(amount, parseFloat(moneyLine, 10), false),
riskSelected: false,
winSelected: true
});
}
});
}
that _.filter
method is doind what I want, is taking out the slip.active != 0
. But, from the background, is there something better to use than the _.filter
method ?
Upvotes: 1
Views: 44
Reputation: 816462
[...] is there something better to use than the
_.filter
method
Absolutely: not using it is much better.
var moneyLine = _.result(_.find(slip.lines, {isSelected: '1'}), 'moneyLine');
if (slip.active == '1') {
_.assign(slip, {
win: amount,
risk: RiskWinCalculations.calculateStraightBet(amount, parseFloat(moneyLine, 10), false),
riskSelected: false,
winSelected: true
});
}
The purpose of _.filter
is to select a subset of a collection. It is not the right function to use if you want to test whether a property has a specific value.
Upvotes: 2