I have simple Vue instance:
<html>
<body>
<div id="container">
<input type="text" id="container" placeholder="enter text" v-model="value">
<p>{{ value }}</p>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/0.11.10/vue.min.js"></script>
<script>
new Vue({
el: '#container',
data: {
value: '',
list: []
},
created: function() {
console.log(typeof this.list); // i would like to determine type of underlaying object
}
});
</script>
</body>
</html>
https://codepen.io/anon/pen/KxVQEw?editors=1111
How to determine the type of the observed proprety in data lets say .list inside 'created' life cycle hook?
typeof [] will return "object", same as typeof {}. If you want to know whether it's for example a JSON object or an array you can use varname.constructor.name:
console.log(typeof []) // object
console.log([].constructor.name) // Array
console.log(typeof {}) // object
console.log({}.constructor.name) // Object
In your case:
console.log(this.list.constructor.name) // Array
Although there already is a working answer, I decided to use this alternative as it is less code: Array.isArray
Array.isArray([]) //true
Array.isArray({}) //false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With