[英]Java Script: Nested Return Statement, return inside Return
In one of my previous question js: Multiple return in Ternary Operator I asked about returning multiple parameter with ternary-operator. 在我之前的一个问题js中:三元运算符中的多个返回我询问了如何使用三元运算符返回多个参数。 But now the parameter IsActveUser boolean(true|false) flag have been modified to integer(0|1|2|3) flag , 但现在参数IsActveUser boolean(true | false)标志已被修改为整数(0 | 1 | 2 | 3)标志 ,
Previously it has only two states, 以前只有两个州,
True - Valid User
False - InValid User
now it has four states, 现在它有四个州,
0 - InValid User
1 - Valid User
2 - Future User
3 - Expired User
So previously, I have coded like, 所以以前,我编码像,
var data = userInfo.map(function (element) {
return {
'LastName': Capitalizefirstletter(element[1]),
'FirstName': Capitalizefirstletter(element[2]),
'UserName': element[3],
'IsActiveUser': ~element[9].search("Valid") ? "True": "False" // really a string?
};
)}
Now I have to check like this, for four status flag 现在我必须像这样检查四个状态标志
var data = userInfo.map(function (item) {
if (item[9].search("title = \"Expired\"")) {
return {
'LastName': Capitalizefirstletter(item[1]),
'FirstName': Capitalizefirstletter(item[2]),
'UserName': item[3],
'UserStatusFlag': 3 // flag should be 3 in this case
};
}
else if (item[9].search("title = \"Valid\"")) {
return {
'LastName': Capitalizefirstletter(item[1]),
'FirstName': Capitalizefirstletter(item[2]),
'UserName': item[3],
'UserStatusFlag': 1 // flag should be 1 in this case
};
}
else if (item[9].search("title = \"Invalid\"")) {
return {
'LastName': Capitalizefirstletter(item[1]),
'FirstName': Capitalizefirstletter(item[2]),
'UserName': item[3],
'UserStatusFlag': 0 // flag should be 0 in this case
};
}
else if (item[9].search("title = \"Future\"")) {
return {
'LastName': Capitalizefirstletter(item[1]),
'FirstName': Capitalizefirstletter(item[2]),
'UserName': item[3],
'UserStatusFlag': 2 // flag should be 2 in this case
};
}
});
but this is not working as expected in Knockout JS. 但这在Knockout JS中没有按预期工作。
Shall I use anything like this, else-if checking inside return satement 我应该使用这样的东西,否则 - 如果检查内部返回声明
var data = userInfo.map(function (element) {
return {
'LastName': Capitalizefirstletter(element[1]),
'FirstName': Capitalizefirstletter(element[2]),
'UserName': element[3],
'UserStatusFlag': if(~element[9].search("Valid")) {1}
else if (~element[9].search("InValid")) {0}
else if (~element[9].search("Future")) {2}
else if (~element[9].search("Expired")) {3}
};
})
Any suggestion would be helpful. 任何建议都会有所帮助。
Make a function call, use a switch statement 进行函数调用,使用switch语句
function getCode (item) {
switch (item) {
case "InValid User" :
return 0;
case "Valid User" :
return 1;
case "Future User" :
return 2;
case "Expired User" :
return 3;
}
}
and call it when you build the object 并在构建对象时调用它
var data = userInfo.map(function (element) {
return {
'LastName': Capitalizefirstletter(element[1]),
'FirstName': Capitalizefirstletter(element[2]),
'UserName': element[3],
'UserStatusFlag': getCode(element[9])
};
});
or you can use an object instead of a switch 或者您可以使用对象而不是开关
var states = {
"InValid User" : 0,
"Valid User" : 1,
"Future User" : 2,
"Expired User" : 3
};
var data = userInfo.map(function (element) {
return {
'LastName': Capitalizefirstletter(element[1]),
'FirstName': Capitalizefirstletter(element[2]),
'UserName': element[3],
'UserStatusFlag': states[element[9]]
};
});
I detest switch
and if
blocks for this. 我厌恶switch
, if
阻止这个。 An object
mapping works so much better: object
映射的效果非常好:
var USER_LOOKUP = {
"InValid User" : 0,
"Valid User" : 1,
"Future User" : 2,
"Expired User" : 3
};
var data = userInfo.map(function (element) {
return {
'LastName': Capitalizefirstletter(element[1]),
'FirstName': Capitalizefirstletter(element[2]),
'UserName': element[3],
'UserStatusFlag': USER_LOOKUP[element[9]]
};
)};
In JavaScript if
is not an expression so it does not have a return value. 在JavaScript中if
不是表达式,因此它没有返回值。 But you can use the ternary operator
(it's acctually called conditional operator
in JavaScript) 但是你可以使用ternary operator
(它在JavaScript中实际上称为conditional operator
)
I assume that the reason why your code is not working is that String.prototype.search
returns the index of the found string or -1 if the string was not found. 我假设你的代码不起作用的原因是String.prototype.search
返回找到的字符串的索引,如果找不到字符串,则返回-1。 Depending on your data you might not need to use search and can just compare the sting directly. 根据您的数据,您可能不需要使用搜索,只需直接比较sting即可。 So instead of 而不是
item[9].search("title = \"Valid\"")
you have to use 你必须使用
item[9].search("title = \"Valid\"") > -1
or you can do what you did in your 2nd example and use the tilde (as explained here ) 或者你可以做你在第二个例子中做的事情并使用代字号(如这里所解释的)
~item[9].search("title = \"Valid\"")
So you could use: 所以你可以使用:
item[9].search('title = "Expired"') > -1 ? 3 :
item[9].search('title = "Valid"') > -1 ? 1 :
item[9].search('title = "Invalid"') > -1 ? 0 :
item[9].search('title = "Future"') > -1 ? 2 :
(()=> { throw Error(item[9] + ' is not a valid state' ) })()
Of course a switch or a map might also work. 当然,开关或地图也可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.