在JavaScript中的条件运算符中执行多个语句



我想在条件运算符内呈现两个数组,第一个数组在第二个数组的顶部呈现,不想连接具有重复元素的数组。

someCondition ? (
arr1.map(elm => renderItem(elm)),
arr2.map(elm => renderItem(elm))
) : null

但是,这只会渲染第二个数组,而忽略第一个数组。

此外,不希望将两个数组连接起来,然后从连接的数组中筛选元素,因为第二个数组有时可能非常大。

您可以在此处使用React Fragments:

someCondition ? (
<>
{arr1.map(elm => renderItem(elm))}
{arr2.map(elm => renderItem(elm))}
</>
) : null

最新更新