How to do check if a string is in the array?

Hi!

I’d like to know how could I check if theres a string in an array on Ionic 4, thank you so much

Regards

Hi, @Manel00
You can do something like this,

if (value instanceof Array) {
   var somethingIsNotString = false;
   value.forEach(function(item){
      if(typeof item !== 'string'){
         somethingIsNotString = true;
      }
   })
   if(!somethingIsNotString && value.length > 0){
      console.log('string[]!');
   }
}

First, it will check whether value is array type or not, if it is of array type then it will check each value stored in the value is a string or not if it is not type of string then it will store true in somethingIsNotString.

Hope it helps…!!

It seems quite correctly, maybe something easier?

I’m trying to add on an array a value with kind of includes or contains method, but cannot make it works

if (this.posts.likes.includes(this.myUserId)) {
            this.heartType = 'heart'
        } else {
            this.posts.likes: firestore.FieldValue.arrayUnion(this.user.getUsername())
        }