object INSIDE 数组的条件渲染
Conditional rendering of object INSIDE array
我有一列 object 辆汽车。我想插入 object with header: AUDI only if audi is true.
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
{header: 'audi',accessor: row},
{header: 'bmw',accessor: row}
]
我尝试使用三元运算符,但汽车不接受空 object {}。那么有没有一种方法可以模拟我们通常做的条件渲染呢? (例如:audi &&{...})谢谢 <3
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
audi?{header: 'audi',accessor: row}:{},
{header: 'bmw',accessor: row}
]
您可以使用 filter
method:
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
{header: 'audi',accessor: row},
{header: 'bmw',accessor: row}
].filter(car => audi || car.header !== 'audi')
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
...(audi ? [
{header: 'audi',accessor: row}
] : []),
{header: 'bmw',accessor: row}
]
我有一列 object 辆汽车。我想插入 object with header: AUDI only if audi is true.
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
{header: 'audi',accessor: row},
{header: 'bmw',accessor: row}
]
我尝试使用三元运算符,但汽车不接受空 object {}。那么有没有一种方法可以模拟我们通常做的条件渲染呢? (例如:audi &&{...})谢谢 <3
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
audi?{header: 'audi',accessor: row}:{},
{header: 'bmw',accessor: row}
]
您可以使用 filter
method:
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
{header: 'audi',accessor: row},
{header: 'bmw',accessor: row}
].filter(car => audi || car.header !== 'audi')
var audi = false;
var cars= [
{header: 'fiat',accessor: row},
...(audi ? [
{header: 'audi',accessor: row}
] : []),
{header: 'bmw',accessor: row}
]