Vue FindIndex Method
Vue Js FindIndex method : you can use the pass condition to find the index of an item in an array using the findIndex()
method in Vue Js . This method searches through the array and returns the index of the first item that matches the testing function. If no items match, it returns -1.

written

reviewed

updated

Thanks for your feedback!
Your contributions will help us to improve service.
By passing a condition, the findIndex method in JavaScript can be used to determine an array item's position.
Example 1: Find the first even number in an array using Vue.js
Copied to Clipboard
xxxxxxxxxx
<script type="module">
import { createApp } from 'vue';
createApp({
data() {
return {
numbers: [33, 15, 38, 69, 45, 70],
firstEvenIndex: ''
};
},
methods: {
findFirstEvenIndex() {
this.firstEvenIndex = this.numbers.findIndex(number => number % 2 === 0);
}
}
}).mount('#app');
</script>
Output of Vue Js Array findIndex Method
Example 2 : Find First Element that is an Odd Number in Vue Js
Copied to Clipboard
xxxxxxxxxx
<script type="module">
import { createApp } from 'vue';
createApp({
data() {
return {
numbers: [32, 51, 83, 96, 54, 707],
result: ''
};
},
methods: {
findOddNumberIndex() {
this.result = this.numbers.findIndex(number => number % 2 === 1);
}
}
}).mount('#app');
</script>
Output of above example
Ad