Home > front end >  in_array() in Vue .js
in_array() in Vue .js

Time:07-23

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>

CodePudding user response:

<template>
        <div>
         <div>testing</div>
            <div v-if="in_array(1)">If condition Yes</div>
        </div>
        </template>

  
  <script>
export default {
  name: "HelloWorld",
  props: {
    msg: String,
  },
  data() {
    return { list: [1, 2, 5, 6, 7] };
  },
  methods: {
    in_array: function (n) {
      var items = this.list.filter((ele) => {
        return ele == n;
      });
      console.log(items);
      if (items.length > 0) {
        return true;
      } else return false;
    },
  },
};
</script>  

CodePudding user response:

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>
  • Related