1

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 ?

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

1 Comment

Ah OK, I didn't realize how straightforward it is . Over complicated it.

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.