我正在将值传递给我的组件,但有些传递的值为“”。这将创建一个视觉容器,该容器在我的视图中为空。如果该值等于“”,我就根本不要通过。
<AddIntel initialValues={{
pastIntelNotes: [
profile.intelNotes,
profile.intelNotes2,
profile.intelNotes3,
profile.intelNotes4
]
}} />
我尝试了有条件的
<AddIntel initialValues={{
pastIntelNotes: [
profile.intelNotes,
profile.intelNotes2 != "" ? profile.intelNotes2 : null,
profile.intelNotes3,
profile.intelNotes4
]
}} />
null
越过了组件,该组件仍将其映射为长度
initialValues:
pastIntelNotes: Array(4)
0: "Notes"
1: null
2: "Notes2"
3: "Notes3"
length: 4
我正在寻找:
initialValues:
pastIntelNotes: Array(3)
0: "Notes"
1: "Notes2"
2: "Notes3"
length: 3
答案 0 :(得分:1)
尝试一下:
const pastIntelNotes = [profile.intelNotes, profile.intelNotes2, profile.intelNotes3, profile.intelNotes4];
<AddIntel initialValues={{
pastIntelNotes: pastIntelNotes.filter(note => note !== ""),
}} />
答案 1 :(得分:1)
使用数组的filter
从数组中删除不需要的值:
initialValues.filter((v) => v !== '').map(..)