4

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?

2 Answers 2

11

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
Sign up to request clarification or add additional context in comments.

1 Comment

I was surprised but it seems to work as i desire for observers.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.