How return true if the array of object doesn’t include object with this Id?

I am trying to find if the array doesn’t include tag with the selected tag id then return true. However this code is not working.

 !this.product.tags.includes(
        tag =>
          tag.type === 'shoes' &&
          tag.Id === this.selectedProduct[item][1].tag.Id,
      )

What is wrong and how can be it fixed?

Answer

includes is the wrong built-in function, you’re looking for some

!this.product.tags.some(
        tag =>
          tag.type === 'shoes' &&
          tag.Id === this.selectedProduct[item][1].tag.Id,
      )

includes checks if the provided argument exists in the given array.

some checks if any of the elements of the array evaluate to true with the provided function.

Leave a Reply

Your email address will not be published. Required fields are marked *