Home > Software design >  Which would be the proper way to iterate this Array of objects and remove the ones with item.text =
Which would be the proper way to iterate this Array of objects and remove the ones with item.text =

Time:11-15

So I have the next Array:

    const contactInfo = computed(() => {
      return [
        {
          id: 'address',
          icon: 'detail/clubLocation',
          text: `${props.data.contactInfo.address}`
        },
        {
          id: 'phone',
          icon: 'contactInfo/phone',
          text: `${props.data.contactInfo.phone}`
        },
        {
          id: 'mail',
          icon: 'contactInfo/mail',
          text: `${props.data.contactInfo.email}`
        },
        {
          id: 'instagram',
          icon: 'contactInfo/instagram',
          text: `${props.data.contactInfo.instagram}`
        },
        {
          id: 'facebook',
          icon: 'contactInfo/facebook',
          text: `${props.data.contactInfo.facebook}`
        }
      ]
    })

And I just got a call from the back that all those fields are optional, so I have to handle the text ⇒ null and undefined/'' I did the next computed and returned it:

  const contactInfoFiltered = computed(() => {
      return contactInfo.value.filter(
        info => info.text !== null || info.text !== ''
      )
    })

But it's not working because I still see the icons being rendered, and I'm not really sure why.
Thanks in advance and of course, any help is highly appreciated!

CodePudding user response:

In js, any value can be tested in order to know if it is "something", so you can test null, undefined or even 0 or '' which will all result in a false evaluation, this can so solve your problem :

const contactInfoFiltered = computed(() => contactInfo.value.filter(info => info))
  • Related