简单地如果其他条件在 JavaScript/es6
Simply if else condition in JavaScript/es6
我如何使它更简单。
if (!request) {
return 'no request';
} else {
if (loading === '404') {
return 'rejected';
}
if (loading === '200' && !array1.length) {
return 'fulfilled';
}
}
如果有任何三元运算符,我该怎么做
你可以省略 else 部分,因为你 return if true
.
if (!request) return 'no request';
if (loading === '404') return 'rejected';
if (loading === '200' && !array1.length) return 'fulfilled';
您也可以简单地使用三元运算符来完成此操作。希望下面的代码对你有用。
!request ? 'no request' : loading === '404' ? 'rejected' : 'fullfilled'
您可以使用if-elseif-else
语句
if(!request) return 'no request'
else if(loading === '404') return 'rejected'
else if(loading === '200' && !array.length) return 'fulfilled'
if (request) {
switch (loading) {
case "404":
return "rejected";
case "200":
if (!array1.length) return "filfilled";
break;
}
} else {
return "no request";
}
考虑到您可能会遇到更多情况,我认为此处使用 switch 语句更好。
有人可能会考虑...
{
// ...
return ((!request && 'no request')
|| (loading === '404' && 'rejected')
|| (loading === '200' && !array1.length && 'fulfilled')
|| undefined // or 'failure' // or 'not fulfilled'
);
}
...或...
{
// ...
let returnValue = (!request && 'no request')
|| (loading === '404' && 'rejected')
|| (loading === '200' && !array1.length && 'fulfilled')
|| undefined; // or 'failure'; // or 'not fulfilled';
// do more stuff ...
// ...
// ... maybe even change `returnValue` again.
return returnValue;
}
我如何使它更简单。
if (!request) {
return 'no request';
} else {
if (loading === '404') {
return 'rejected';
}
if (loading === '200' && !array1.length) {
return 'fulfilled';
}
}
如果有任何三元运算符,我该怎么做
你可以省略 else 部分,因为你 return if true
.
if (!request) return 'no request';
if (loading === '404') return 'rejected';
if (loading === '200' && !array1.length) return 'fulfilled';
您也可以简单地使用三元运算符来完成此操作。希望下面的代码对你有用。
!request ? 'no request' : loading === '404' ? 'rejected' : 'fullfilled'
您可以使用if-elseif-else
语句
if(!request) return 'no request'
else if(loading === '404') return 'rejected'
else if(loading === '200' && !array.length) return 'fulfilled'
if (request) {
switch (loading) {
case "404":
return "rejected";
case "200":
if (!array1.length) return "filfilled";
break;
}
} else {
return "no request";
}
考虑到您可能会遇到更多情况,我认为此处使用 switch 语句更好。
有人可能会考虑...
{
// ...
return ((!request && 'no request')
|| (loading === '404' && 'rejected')
|| (loading === '200' && !array1.length && 'fulfilled')
|| undefined // or 'failure' // or 'not fulfilled'
);
}
...或...
{
// ...
let returnValue = (!request && 'no request')
|| (loading === '404' && 'rejected')
|| (loading === '200' && !array1.length && 'fulfilled')
|| undefined; // or 'failure'; // or 'not fulfilled';
// do more stuff ...
// ...
// ... maybe even change `returnValue` again.
return returnValue;
}