javascript - 三元运算符,希望在 JavaScript 中使用多个三元运算符
问题描述
在这部分代码中,这是我在底部发布的完整代码的一部分,
{
this.props.program.application_required ||
this.state.isEnableWaitlist ||
this.isFree() ? ( <
button type = "button"
onClick = { _ => this.manualPayment() }
style={{
padding: "10px",
display: "block",
marginLeft: "auto",
marginBottom: 0
}}>
{
this.props.program.application_required ||
this.state.isEnableWaitlist ?
"Submit Application" :
"Confirm Order"
} </button>
)
我想在同一个 if 语句中this.state.isEnableWaitlist
与其他两个this.props.program.application_required
&分开,以便在它遇到以this.isFree()
this.state.isEnableWaitlist
: (<>
<p style = {{
padding: "20px",
fontFamily: "Open Sans, sans-serif"
}}.
在三元运算符中最好的方法是什么?
这是下面的完整代码
{
this.props.program.application_required ||
this.state.isEnableWaitlist ||
this.props.program.manual_invoices ||
this.isFree() ? (<> {
this.props.program.application_required ||
this.state.isEnableWaitlist ||
this.isFree() ? ( <
button type = "button"
onClick = { _ => this.manualPayment() }
style = {{
padding: "10px",
display: "block",
marginLeft: "auto",
marginBottom: 0
}} >
{
this.props.program.application_required ||
this.state.isEnableWaitlist ?
"Submit Application" :
"Confirm Order"
} </button>
) : ( <>
<p style = {{
padding: "20px",
fontFamily: "Open Sans, sans-serif"
}} >
<b> {
this.props.organization
} </b> opted to collect
payments manually.After confirming your order {
" "
} <b> {
this.props.organization
} </b> will contact you to
discuss next steps & payment options. </p>
<
button type = "button"
onClick = { _ => this.manualPayment() }
style = {{
padding: "10px",
display: "block",
marginLeft: "auto",
marginBottom: 0
}} >
Confirm Order </button> </>
)
} </>
)
解决方案
虚假值
[0, "", '', false, null, undefined]
三元
condition ? yes : no;
condition
? 1
: condition2
? 1
: 0
逻辑与
12 && "hello" && null && undefined // return first falsy value
逻辑或
12 || "hello" || null || undefined // return first truthy value
无效合并
0 ?? "hello" // 0
false ?? "hello" // false
逻辑无效赋值
// Normal
let x = null;
if (x === null || x === undefined) {
x = 12;
}
// Advanced
let x = null;
x ??= 12;
逻辑或赋值
// Normal
let x = false;
if (!x) {
x = 12;
}
// Advanced
let x = false;
x ||= 12;
逻辑与赋值
// Normal
let x = "Truthy value";
if (x) {
x = 12;
}
// Advanced
let x = "Truthy value";
x &&= 12;
推荐阅读
- flask-socketio - 烧瓶重定向不起作用 - 至少我会
- scala - How spark shuffle partitions and partition by tag along with each other
- elasticsearch - Kibana 可执行文件不兼容
- r - 在 R 中基于 filter_at 创建一个新列
- machine-learning - 微观 VS 宏观 VS 加权 F1 分数
- apache-kafka - 如何在 ConsumerInterceptor 中访问 org.apache.kafka.clients.consumer.Consumer?
- reactjs - Redux saga 调用多次(2 或 3 次)Nextjs
- r - Calculate difference from the group mean using dplyr
- r - 如何匹配一组字符串中的一个字符串,然后是一些数字,然后是另一个字符串?
- windows - 在寡妇 10 中使用命令行编辑 PATH 变量