Vue js convert String to Array – In Vue.js, you can convert a string to an array using the JavaScript split method. The split method takes a separator as an argument and returns an array of substrings separated by that separator. For example, if you have a string “apple,banana,orange” and you want to convert it to an array, you can use the split method with the comma separator like this: “apple,banana,orange”.split(“,”). This will return an array [“apple”, “banana”, “orange”]. You can then use this array in your Vue.js application as needed.
Vue js convert String to Array Example
You can use split method in vue to split string by comma simply as below-
VueJs Split String Function Example
<script type="importmap">
{
"imports": {
"vue": "https://unpkg.com/vue@3/dist/vue.esm-browser.js"
}
}
</script>
<div id="app">
<button @click="convertToArray" >Convert To Array</button>
<p>String = {{testString}}</p>
<p>Array = {{testArray}}</p>
</div>
<script type="module">
import { createApp } from 'vue'
createApp({
data() {
return {
testString: "1 2 3 4 5 6 7 8 9",
testArray: [],
}
},
methods:{
convertToArray(){
this.testArray = this.testString.split(" ");
}
}
}).mount('#app')
</script>