Vue.js remove Array item By Value


We can remove array item by value using the indexof item. Here in this tutorial, we are going to explain how you can remove item by value in vue.js. In this tutorial we are going to create an array in which we will remove item by specified value ie element. You can also use our online editor to edit and run the code online.


Vue.js remove Array item By Value Example

Here is an example to remove an element by value from an array-

Example:

<div id="app">	
	<p>{{myArray}}</p>
	<p>Click to Remove "Banana"</p>
	<button @click="myFunction()">Click Me</button>
</div>
<script>
 new Vue({
el: '#app',
  
  data: { 
	  myArray:["Apple", "Banana", "Mango"],
	  finalArray:''
  },
  
   methods:{
    myFunction: function () {	
		var index = this.myArray.indexOf("Banana");
		if(index > -1){
		this.myArray.splice(index, 1);
		}
    }
   }

});
</script> 

Try it »

Output of above example-

Vue.js remove Array item By Value

Advertisements

Add Comment

📖 Read More