Vue.js Array Find
JavaScript Find method is used to find the value in array based on test(provided as function). Here in this tutorial, we are going to explain how you can use this method in Vuejs. You can also use our online editor to edit and run the code online.
Vue.js Array Find Method Example
You can use find method in vue.js simply as below-
Example:
<div id="app"> <p>item 1 = {{ item1 }}</p> <p>Result = {{ item2 }}</p> <button @click="myFunction()">Click Me</button> </div> <script> new Vue({ el: '#app', data: { item1:['12', '10', '9','5', '6', '4'], item2:[] }, methods:{ myFunction: function () { this.item2 = this.item1.find(this.checkNum); }, checkNum:function(num){ if(num > 9){ return num; } } } }); </script> |
The find function will test the array value based on function checkNum and return the first matching value.
Output of above example-
Advertisements