Vue.js Array Unshift


This method adds the new elements at the beginning of array. Here in this tutorial, we are going to explain how you can use this function to add new elements at beginning of an array in vue.js. You can also use our online editor to edit and run the code online.


Vue.js Array Unshift Method Example

You can use the unshift method in vuejs array simply as below-

Example:

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

});
</script> 

Try it »

Output of above example-

Vue.js Arrya unshift Example & demo

Advertisements

Add Comment

📖 Read More