Javascript JSX:映射中的条件语句
我试图理解如何在JSX中合并条件语句。我有一个映射到选择框的对象数组。我想排除在indicator.name中包含子字符串“threshold”的项目 所以我不能使用三元运算符,因为没有“或”项。但是我不知道如何在这个映射中包含if语句。无论我做什么,我都会出错Javascript JSX:映射中的条件语句,javascript,reactjs,jsx,Javascript,Reactjs,Jsx,我试图理解如何在JSX中合并条件语句。我有一个映射到选择框的对象数组。我想排除在indicator.name中包含子字符串“threshold”的项目 所以我不能使用三元运算符,因为没有“或”项。但是我不知道如何在这个映射中包含if语句。无论我做什么,我都会出错 setIndicator(e.currentTarget.value)} 已禁用={loading} > 挑选 {indicatorList.map(指示符=>( {indicator.label} ))} 我建议使用过滤方法过滤掉“
setIndicator(e.currentTarget.value)}
已禁用={loading}
>
挑选
{indicatorList.map(指示符=>(
{indicator.label}
))}
我建议使用过滤方法过滤掉“阈值”。这将返回您希望在JSX中包含的数组,您可以筛选该数组,然后映射:
<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>
setIndicator(e.currentTarget.value)}
已禁用={loading}
>
挑选
{indicatorList.filter(indicator=>indicator.name.includes('threshold')).map(indicator=>(
{indicator.label}
))}
或返回空值:
<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>
setIndicator(e.currentTarget.value)}
已禁用={loading}
>
挑选
{indicatorList.map(指示符=>(
指示符.名称.包括('threshold')?:num
{indicator.label}
))}
indicatorList.filter(v=>v.includes(“阈值”)).map(…
谢谢!过滤然后映射是我所需要的