2015-06-01 27 views
1

我想使我的if語句靈活。如果我在輸入框中輸入一個確切的信息,我的語句會觸發,但如果用戶以小寫或大寫字母輸入,則無法檢測到。這裏是代碼。如果語句檢查不考慮信件的情況

var find = _.findWhere($scope.allCast, {name: castName}); 
      if(!find){ 
       var cast = { 
        cpPortfolioItemId: id, 
        name: castName, 
        job: 'cast', 
        role: castRole 
       }; 
       ContentAssessmentFactory.addCastDetail(cast); 
      }else{ 
       $window.alert('Cast name is already exist.'); 
      } 

任何幫助,將這麼多的讚賞。

回答

1

可以使用_.filter()與變換在較低或較高的情況下cast.namecastName

//Return you an array of matched elements 
var find = _.filter($scope.allCast, function(cast){ 
    //Convert both text in lower case and compare 
    //If required you can use .trim() like castName.trim().toLowerCase() to strip whitespace 
    return cast.name.toLowerCase() == castName.toLowerCase(); 
}); 

if(find.length == 0){ 
    var cast = { 
     cpPortfolioItemId: id, 
     name: castName, 
     job: 'cast', 
     role: castRole 
    }; 
    ContentAssessmentFactory.addCastDetail(cast); 
}else{ 
    $window.alert('Cast name is already exist.'); 
} 
+0

謝謝!這實際上起作用。但我能問嗎? _.find和_.findWhere有什麼不同?我可以在_.findWhere上做同樣的功能嗎? –

+0

@MixAustria,'_.findWhere'不支持作爲'_.filter'使用的函數'predicate'。你使用'underscore'或'lodash'是哪個庫?如果想通過文檔https://lodash.com/docs – Satpal