gpt4 book ai didi

reactjs - 如何使用三元运算符来显示一些文本,而不是在 react 和 typescript 中使用 if else 条件?

转载 作者:行者123 更新时间:2023-12-04 07:15:39 25 4
gpt4 key购买 nike

我有如下代码,

if (filteredIds.length > 0) {
notify({
title: `${
type === "type1"
? 'type1 checks'
: 'type2 checks'
} started.`,
});
} else {
notify({
title: `${
type === "type1"
? 'type1 checks'
: 'type2 checks'
} have been already running on selected id.`,
});
}
上面的代码有效。但我想使用三元运算符而不是 if 和 else ,因为通知事情似乎是重复的。
我想要一些东西,比如让三元运算符通知自己根据filteredIds的长度和类型返回必要的文本。
notify({
title: `${ //condition here
type === "type1"
? 'type1 checks'
: 'type2 checks'
} started.`,
});
基本上我希望 if else 代码更少。有人可以帮我解决这个问题。我是编程新手,不确定如何使用嵌套三元运算符。
谢谢。

最佳答案

您是否正在寻找这样的条件表达式?

notify({
title: `${
type === "type1"
? 'type1 checks'
: 'type2 checks'
} ${filteredIds.length? 'started' : 'have been already running on selected id'}.`,
});

关于reactjs - 如何使用三元运算符来显示一些文本,而不是在 react 和 typescript 中使用 if else 条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68780989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com