Vue.js check variable is array or object


You can check whether a variable is array or object using native JavaScript typeOf check. Here in this tutorial, we are going to explain how you can use this check in vue.js to decide if variable is array or object.


Vue.js check variable is array or object JavaScript Example

You can check variable is object or array in Vue.js simply as below-

Example:

<div id="app">
	<p>Result 1 = {{result1}}</p>
	<p>Result 2 = {{result2}}</p>
	<button @click="myFunction()">Click Me</button>
</div>
<script>
 new Vue({
 el: '#app',
  
  data: {
	  array1: [1, 9, 10],
	  obj1: {type:"Fiat", model:"800"},
	  result1:'',
	  result2:''
  },  
   methods:{
    myFunction: function () {
	 this.result1 = Array.isArray(this.array1);
	 this.result2 = this.obj1 instanceof Object;
    }
   }
});
</script> 

Try it »

Output of above example-

Vue.js check variable is array or object

Advertisements

Add Comment

📖 Read More