0

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Please stop adding the [ecmascript-5] tag to questions that are not about ECMAScript 5. This question uses `includes` and arrow functions, both of which were introduced in ECMAScript 2015, also known as ECMAScript 6. This question is also not about ECMAScript 6; it just happens to use constructs introduced in that version; it does not need the tag. – Heretic Monkey Mar 08 '21 at 17:14
  • 1
    Does this answer your question? [How to determine if Javascript array contains an object with an attribute that equals a given value?](https://stackoverflow.com/questions/8217419/how-to-determine-if-javascript-array-contains-an-object-with-an-attribute-that-e) – Heretic Monkey Mar 08 '21 at 17:17

2 Answers2

1

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.

Dane Brouwer
  • 2,827
  • 1
  • 22
  • 30
0
 !this.product.tags.includes.some(
        tag =>
          typeof( tag.type )=== (typeOfvar) &&
          tag.Id === this.selectedProduct[item][1].tag.Id,
      )

works for me too! i have changed the tag.type with the typeof. This can helps you to make to code more dinamic.

Modena
  • 75
  • 8