Vue.js Array findIndex


JavaScript function findIndex returns the index of first element which pass the test based on function. Here in this tutorial, we are going to explain how you can use this method to find the index of first element based on the provided function to check the array elements. You can also use our online editor to edit and run the example online.


Vue.js Array findIndex Method Example

You can get index of array element based on test in vuejs simply as below-

Example:

<div id="app">
	<p>item 1 = {{ item1 }}</p>
	<p>Result = {{ 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.findIndex(this.checkNum);
		
    },
	checkNum:function(num){
	   if(num < 9){
	    return num;
       }
     }
	}

});
</script> 

Try it »

Output of above example-

Vue.js Array findIndex Exmaple

Advertisements

Add Comment

📖 Read More