How to convert string to array in VueJS?
April 30, 2023Hi Friends đź‘‹,
Welcome To SortoutCode! ❤️
To convert string to array in VueJS. Sometimes we need to convert the string to an array. There are different ways to convert the string to an array. In this We are going to use the split()
, Array.from()
, and Array.prototype.push()
methods to convert the string to an array.
Short solution
parentComp.vue
var Arry = "Monday,Tuesday,Sunday"; //string
// using the split() method
arrA = Arry.split(",");
console.log(arrA); // ['Monday',' Tuesday','Sunday']
// using the Array.from() method
arrB = Array.from(this.str.split(","));
console.log(arrB); // ['Monday',' Tuesday','Sunday']
let newArray = [];
let arrC = "";
for (let i = 0; i < Arry.length; i++) {
if (Arry[i] !== ",") {
temp += Arry[i];
} else {
newArray.push(temp.trim());
temp = "";
}
}
if (temp. length) {
newArray.push(temp.trim());
}
console.log(newArray);
); // ['Monday',' Tuesday','Sunday']
Today, I am going to show you How you convert string to array in VueJS?
Table of Content
- Setup the Vue (Optional)
- Create FirstComponent.vue and import it into App.js
- Convert string to an array using split() method
- Convert string to an array using Array.from() method
- Convert string to an array using Array.prototype.push() method
This article will guide you to How do I convert string 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 string to array using split() method
To convert the string to an array in vuejs. We are going to define the long string logString
in the data property and defined the newArray
empty array data property. And we are going to use the split()
method. This method will convert the string into an array. Let’s take a short example to better understand.
FirstComp.vue
<template>
<div class="hello"></div>
<h1>Welcome to sortoutcode.com</h1>
<p>Long String : {{ logString }}</p>
<p v-if="newArray">Array : {{ newArray }}</p>
<button @click="convertMethodA">Convert</button>
</template>
<script>
export default {
name: "FirstComp",
data() {
return {
logString: "magzine, sortout, google, youtube",
newArray: [],
};
},
methods: {
convertMethodA() {
this.newArray = this.logString.split(", "); //[ "magzine", "sortout", "google", "youtube" ]
},
},
};
</script>
For now, let’s check the output.
Output
Convert string to an array using Array.from() method
To convert the string to an array in vuejs. We are going to define the long string logString
in the data property and defined the newAArray
empty array data property. And we are going to convert this string to an array using the Array.from()
method. This method will convert the string into an array. Let’s take a short example to better understand.
FirstComp.vue
<template>
<div class="hello"></div>
<h1>Welcome to sortoutcode.com</h1>
<p>Long String : {{ logString }}</p>
<p v-if="newArray">Array : {{ newArray }}</p>
<button @click="convertMethodB">Convert</button>
</template>
<script>
export default {
name: "FirstComp",
data() {
return {
logString: "magzine, sortout, google, youtube",
newArray: [],
};
},
methods: {
convertMethodB() {
this.newArray = Array.from(this.logString.split(", ")); //[ "magazine", "sortout", "google", "youtube" ]
},
},
};
</script>
For now, let’s check the output.
Output
Convert string to an array using Array.prototype.push() method
To convert the string to an array in vuejs. We are going to define the long string logString
in the data property and defined the newAArray
empty array data property. And we are going to convert this string to an array using the Array.prototype.push()
method. This method will convert the string into an array. Let’s take a short example to better understand.
FirstComp.vue
<template>
<div class="hello"></div>
<h1>Welcome to sortoutcode.com</h1>
<p>Long String : {{ logString }}</p>
<p v-if="newArray">Array : {{ newArray }}</p>
<button @click="convertMethodC">Convert</button>
</template>
<script>
export default {
name: "FirstComp",
data() {
return {
logString: "magzine, sortout, google, youtube",
newArray: [],
};
},
methods: {
convertMethodC() {
let temp = "";
for (let i = 0; i < this.logString.length; i++) {
if (this.logString[i] !== ",") {
temp += this.logString[i];
} else {
this.newArray.push(temp.trim());
temp = "";
}
}
if (temp. length) {
this.newArray.push(temp. trim()); //[ "magazine", "sortout", "google", "youtube" ]
}
},
},
};
</script>
For now, let’s check the output.
Output
Links
Here, is the above program code sandbox link of how to convert a string to an array using the toString()
, join()
, and for loop
of these three methods in vue js. Then you can use it whenever you went and do the changes as per your requirements.
All the best 👍.