Zak Micallef
Zak Micallef

Reputation: 61

how to access object methods of objects in vue

I am trying to get all the keys from an object in Vue.

I already know to do this in JavaScript. i.e;

const object1 = {
    a: 'somestring',
    b: 42,
    c: false
};

console.log(Object.keys(object1));

The output would be an array.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

How would you do this in a methods in Vue ?

methods: {
    getKeys(object1){
        ...
    },
},

And explain how would you confront other differences like this ?

Upvotes: 1

Views: 1981

Answers (1)

Vucko
Vucko

Reputation: 20844

Bind the method to an event:

new Vue({
  el: "#app",
  data: {
    object1: {
      a: 'somestring',
      b: 42,
      c: false
    }
  },
  methods: {
    getKeys(object) {
      console.log(Object.keys(object));
    }
  }
})
<script src="https://cdn.jsdelivr.net/npm/vue"></script>

<div id="app">
  <button @click="getKeys(object1)">getkeys</button>
</div>

Upvotes: 1

Related Questions