我在一个对象中有两个键。我正在检查以下三种情况
我正在使用下面的工作代码,但想知道是否有更好的方法以更简单的方式执行此操作。
var obj = {
statusUrl: "",
errorUrl: ""
};
function validate(key) {
if (!obj.hasOwnProperty(key) || obj[key] === "") {
return 1;
}
}
var flag = validate("statusUrl");
if (flag) {
var result = validate("errorUrl") ? "failure" : "success";
}
console.log(result)
答案 0 :(得分:0)
您可以像这样简化代码
@Before
public void init() {
final Matchday matchday = matchdayRepository.save(new Matchday(1));
final Club club1 = clubRepository.save(new Club("Klub1"));
final Club club2 = clubRepository.save(new Club("Klub2"));
final Match match = matchRepository.save(new Match(matchday, club1, club2));
final Player player = new Player(1, "Jan", "Kowalski", club1);
player.addPlayerStatistics(new PlayerStatistics(matchday, player, match));
playerRepository.save(player);
}
答案 1 :(得分:0)
总结一下您的要求,statusUrl或errorUrl值意味着成功,否则意味着失败。 <怎么样
fnDiary = [ mfilename '.out.txt' ]
system(['rm -f ' fnDiary])
diary off; diary fnDiary
答案 2 :(得分:0)
var obj = {
statusUrl: "",
errorUrl: ""
};
var result = "failure";
if (obj.statusUrl) {
result = 'success';
} else {
if (obj.errorUrl) {
result = 'success';
}
}
console.log(result);
答案 3 :(得分:0)
这是一个更简单的&#34;方式
var obj = {
statusUrl: "",
errorUrl: ""
},
result = validate(obj);
console.log(result);
function validate(obj){
if (obj.statusUrl !== "") {
//1. if statusUrl value is having some value, then success
return "success"
} else if(obj.errorUrl !== "") {
//2. if statusUrl value is "", errorUrl value must not be ""
return "success"
} else {
//3. if both statusUrl and errorUrl are empty, then failure
return "failure"
}
}
检查truthy/falsey
值时要小心。
var a = "0",
b = 0;
console.log(!a); //false
console.log(!b); //true
答案 4 :(得分:0)
试试这个:
var obj = {
statusUrl: "",
errorUrl: ""
};
function validate(obj) {
if (obj.statusUrl != "" || (obj.statusUrl != "" && obj.errorUrl != "") || (obj.statusUrl == "" && obj.errorUrl != "")) {
return 1;
}
}
var output = validate(obj) ? "success" : "failure";
console.log(output);
答案 5 :(得分:0)
使用它:
var result=(obj.statusUrl && obj.statusUrl!=""?"success":(obj.errorUrl && obj.errorUrl!=""?"success":"failure"))
console.log(result)