-2

I get this error : "TypeError: _vm.in_array is not a function" when I use in_array() function.

Is there any similar function like in_array() in Vue.js?

I want to do something like this and the result should display abc.

<template v-if="in_array(number,list)">
  <h3> abc </h3>
</template>

<script>
  data() {
        return {
          list: [1,2,3],
        }
  },
</script>
Anno123
  • 67
  • 6
  • Where is your `number` declared? You could have used `computed properties` for that purpose. Using complex logic in viewing template is not best practice – Huy Phạm Jul 22 '22 at 10:41
  • 1
    Not Vue.js precisely, but in js, the _arrays_ have method [__includes()__](https://developer.mozilla.org/pl/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) which return true or false depending if the array contains a specific value. In your case it would looks like this __list.includes(number)__ – TymoteuszLao Jul 22 '22 at 11:04

1 Answers1

-1

in_array is not a function in JavaScript, you can use includes in a computed property to make this work tho.

Pseudocode:

<template v-if="in_array">
  <h3> abc </h3>
</template>

<script>
  data() {
    return {
      number: 1,
      list: [1,2,3],
    }
  },
  computed: {
    in_array() {
      return this.list.includes(this.number)
    }
  }
</script>
S.Visser
  • 4,645
  • 1
  • 22
  • 43