Vue.js Array IndexOf


The indexOf method is used to search an specified item in array and returns the position of item. This method returns -1 if item is not found else the position of an item. If occurrence of item is more than once, it will return the index of first index. Here in this tutorial, we are going to explain how to use indexOf Method in vue.js. You can also use our online editor to edit and run the demo online.


Vue.js Array IndexOf Method Example

Here is an example of IndexOf Method in Vue.js-

Example:

<div id="app">
	<p>item 1 = {{ item1 }}</p>
	<p>Element Index = {{elmnIndex}}</p>

	<button @click="myFunction()">Click Me</button>
</div>
<script>
 new Vue({
el: '#app',
  
  data: { 
	  item1:['12', '10', '9','5', '6', '4'],
	  elmnIndex:''
  },
  
   methods:{
    myFunction: function () {		
		this.elmnIndex = this.item1.indexOf('9');
		
    }
	}

});
</script> 

Try it »

Output of above example-

Vue.js array IndexOf Example

Advertisements

Add Comment

📖 Read More