gpt4 book ai didi

javascript - JSX: map 内的条件语句

转载 作者:行者123 更新时间:2023-12-03 14:22:08 24 4
gpt4 key购买 nike

我试图了解如何在 JSX 中合并条件语句。我有一个对象数组,我将其映射到选择框。我想排除在indicator.name中包含子字符串“threshold”的项目。

所以我不能使用三元运算符,因为没有“或”项。但我不知道如何在此映射中包含 if 语句。无论我尝试什么,我都会收到错误。

<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.map(indicator => (
<option key={indicator.name} value={indicator.name}>
{indicator.label}
</option>
))}
</select>

最佳答案

您可以过滤然后映射:

<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.filter(indicator=>indicator.name.includes('threshold')).map(indicator => (
<option key={indicator.name} value={indicator.name}>
{indicator.label}
</option>
))}
</select>

或者返回null:

<select
defaultValue={defaultValue}
onChange={e => setIndicator(e.currentTarget.value)}
disabled={loading}
>
<option key='' value=''>
Select
</option>
{indicatorList.map(indicator => (
indicator.name.includes('threshold')?<option key={indicator.name} value={indicator.name}>:nul
{indicator.label}
</option>
))}
</select>

关于javascript - JSX: map 内的条件语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61219624/

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