How to convert the object to an array in VueJS?

Hi Friends đź‘‹,

Welcome To SortoutCode! ❤️

To convert the object to an array in VueJS. There are other methods to convert objects to the array, we are going to use the Object.keys(Obj), Object.values(Obj) and Object.entries(Obj) three methods. In these methods Obj stand for your object data.

Short solution

parentComp.vue

var Obj = {id:1,sitename:'sortoutcode'}; //object

console.log(Object.keys(Obj)); // ["id","sitename"]

console.log(Object.values(Obj)); // [1,"sortoutcode"]

console.log(Object.entries(Obj)); // [["id",1],["sitename",'sortoutcode']]

Today, I am going to show you How you convert the object to a string in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • Convert an object to an array using Object.keys()
  • Convert an object to an array using Object.values()
  • Convert an object to an array using Object.entries()

This article will guide you to How do I convert the object to array in vueJS

Setup the Vue.js

First, we have to install the Vue project, I had installed the vueJS in my system. If you haven’t installed or have any problem with installation you can follow this article, it will show you step by step process of installation.

How to Install the VueJS project?

Create FirstComponent.vue and import it into App.js

Create the new component in the src/components/ components folder. The component name is FirstComponent.vue and imported into the App.js file:

App.js

<template>
  <FirstComp />
</template>

<script>
import FirstComp from "./components/FirstComp.vue";
export default {
  name: "App",
  components: {
    FirstComp: FirstComp,
  },
};
</script>

Convert an object to array using Object.keys()

To convert object to array in vuejs. We are going to define the object Obj in the data property. And we are going to convert this object into a array using the Object.keys(). It will convert object keys into a new array.

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <div>
      <p>Obj: {{ Obj }}</p>
      <p v-if="output">Output: {{ output }}</p>
      <button @click="convertA">Convert</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      Obj: { id: 1, sitename: "sortoutcode", url: "https://sortoutcode.com/" },
      output: "",
    };
  },
  methods: {
    convertA() {
      this.output = Object.keys(this.Obj);
    },
  },
};
</script>

For now, let’s check the output.

Output

Convert Object to key array

Convert an object to array using Object.values()

To convert object to array in vuejs. We are going to define the object Obj in the data property. And we are going to convert this object into a array using the Object.values(). It will convert object values into a new array.

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <div>
      <p>Obj: {{ Obj }}</p>
      <p v-if="output">Output: {{ output }}</p>
      <button @click="convertA">Convert</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      Obj: { id: 1, sitename: "sortoutcode", url: "https://sortoutcode.com/" },
      output: "",
    };
  },
  methods: {
    convertA() {
      this.output = Object.values(this.Obj);
    },
  },
};
</script>

For now, let’s check the output.

Output

Convert Object to array

Convert an object to an array using Object.entries()

To convert the object to an array in vuejs. We are going to define the object Obj in the data property. And we are going to convert this object into an array using the Object.entries(). It will convert the object key and value into a new array.

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <div>
      <p>Obj: {{ Obj }}</p>
      <p v-if="output">Output: {{ output }}</p>
      <button @click="convertA">Convert</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      Obj: { id: 1, sitename: "sortoutcode", url: "https://sortoutcode.com/" },
      output: "",
    };
  },
  methods: {
    convertA() {
      this.output = Object.entries(this.Obj);
    },
  },
};
</script>

For now, let’s check the output.

Output

Convert Object to array

Links

Here, is the above program code sandbox link of how to convert the object to an array in vue js. Then you can use it whenever you went and do the changes as per your requirements.

Codesandbox

All the best đź‘Ť.

Follow me on Twitter