HDTuto.com

Convert String to Array in VueJS Example



I will let you know how to convert comma separated string to array using split() in vue js. we will convert string to array in vuejs. i will give you simple example of converting string to array by comma, dot, semicolon, space etc.

If you worked with javascript then you know split() and how it use. The split() method is used to split the string into an array of the substring and returns a new collection array. you can see bellow following syntax:

myArray.split(separator);

bellow i gave you simple example of convert string into an array with space. i will also show you output of array, so you can understand how it works.

Example:

<!DOCTYPE html>

<html>

<head>

<title>Vue JS Convert String to Array | Vue JS Split String Example - HDTuto.com</title>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>

<body>

<div id="app">

<p>String : {{ myString }}</p>

<p>Array : {{myResult}}</p>

<button @click="clickFunction()">Click Me</button>

</div>

<script type="text/javascript">

var app = new Vue({

el: '#app',

data: {

myString: "This is HDTuto.com" ,

myResult: ''

},

methods:{

clickFunction: function () {

this.myResult = this.myString.split(" ");

}

}

})

</script>

</body>

</html>

Output:

String : This is HDTuto.com

Array : [ "This", "is", "HDTuto.com" ]

I hope it can help you...




September 1, 2019 | Category : Vue.js
Related Posts



Blog Search