在 React Native 中将嵌套 if/else 转换为三元
Convert nested if/else to ternerary in React Native
向我解释如何将现有的嵌套 if/else 转换为三元运算符。我查看了文档,但仍然无法实现我所需要的。
if(value){
if((value).match('google')) {
return 'Google'}
else if((value).match('apple')){
return 'Apple'}
else 'Other websites'
} else{
return 'Number'
}
这里的{value}是来自JSON的数据。 {value} 可以是 link 或数字。
我想检查 link 是否包含 google,所以显示 'google' 文本。如果它包含苹果,那么给我看苹果文字。如果其他人只是显示 'others website' 文本。如果这不是 link,只显示 'number' 文本
首先检查值?
如果值存在然后检查值是 'google'?如果是 return:
如果值不是 google 检查值是 'apple' 吗?如果是 return:
如果值不是苹果 return 否则:
如果没有值则 return 'Number'
let value = 'apple'
let res = value? value.match('google')? 'Google':value.match('apple')?'Apple':'Otherwebsite':'Number';
console.log(res)
const val = value ? (value).match('google') ? 'Google' : (value).match('apple') ? 'Apple' : 'Other websites' : 'Number';
试试这个。
function func(value) {
return value ? value.match('google') ? 'Google' : value.match('apple') ? 'Apple' : 'Other websites' : 'Number';
}
console.log(func('google'));
console.log(func('apple'));
console.log(func('none'));
console.log(func());
const val = isNaN(value) ? (value.match('google') ? "Google": ( value.match("apple")? "Apple": "Other Websites" )): "Number";
向我解释如何将现有的嵌套 if/else 转换为三元运算符。我查看了文档,但仍然无法实现我所需要的。
if(value){
if((value).match('google')) {
return 'Google'}
else if((value).match('apple')){
return 'Apple'}
else 'Other websites'
} else{
return 'Number'
}
这里的{value}是来自JSON的数据。 {value} 可以是 link 或数字。 我想检查 link 是否包含 google,所以显示 'google' 文本。如果它包含苹果,那么给我看苹果文字。如果其他人只是显示 'others website' 文本。如果这不是 link,只显示 'number' 文本
首先检查值?
如果值存在然后检查值是 'google'?如果是 return:
如果值不是 google 检查值是 'apple' 吗?如果是 return:
如果值不是苹果 return 否则:
如果没有值则 return 'Number'
let value = 'apple'
let res = value? value.match('google')? 'Google':value.match('apple')?'Apple':'Otherwebsite':'Number';
console.log(res)
const val = value ? (value).match('google') ? 'Google' : (value).match('apple') ? 'Apple' : 'Other websites' : 'Number';
试试这个。
function func(value) {
return value ? value.match('google') ? 'Google' : value.match('apple') ? 'Apple' : 'Other websites' : 'Number';
}
console.log(func('google'));
console.log(func('apple'));
console.log(func('none'));
console.log(func());
const val = isNaN(value) ? (value.match('google') ? "Google": ( value.match("apple")? "Apple": "Other Websites" )): "Number";