我想使我的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.');
}
任何帮助将不胜感激。
最佳答案
您可以使用_.filter()
转换小写或大写形式的cast.name
和castName
。
//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.');
}