简体   繁体   中英

Proper lodash method

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 ?

[...] 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM