Не передавайте значение компоненту, если строка равна "" пусто - PullRequest
0 голосов
/ 22 октября 2018

Я передаю значения своему компоненту, но некоторые имеют значение "".Это создает визуальный контейнер, который на мой взгляд пуст.Мне нужно, чтобы значение вообще не передавалось, если оно равно "".

<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

Ответы [ 2 ]

0 голосов
/ 22 октября 2018

Используйте Array's filter для удаления нежелательных значений из массива:

initialValues.filter((v) => v !== '').map(..)
0 голосов
/ 22 октября 2018

Попробуйте это:

const pastIntelNotes = [profile.intelNotes, profile.intelNotes2, profile.intelNotes3, profile.intelNotes4];

<AddIntel initialValues={{
   pastIntelNotes: pastIntelNotes.filter(note => note !== ""),
}} />
...