Solved: convert string to array in vue js

Vue.js has become a remarkably popular JavaScript framework due to its approachable nature and versatility. It is a tool of choice for many web developers when it comes to simplifying web interface development. One common task developers often encounter when working with Vue.js is converting strings to arrays.

This process is vital in instances where you obtain data in a string format but require these data in an array form for more convenient manipulation. Before diving into the solution, it’s best to have basic knowledge of both Vue Js and JavaScript.

Converting a String into an Array in Vue.js

To convert a string to an array in Vue.js, there is a straightforward, built-in method available in JavaScript. It’s called `split()`

var str = "Hello World";
var res = str.split(" ");

The `split()` function is a built-in function in JavaScript that turns a String object into an array of strings by separating the string at each instance of a specified separator string.

Step-by-Step Explanation of the Code

Here, “Hello World” represents a string that needs conversion into an array. The split method applied on the string variable takes an argument, which is a separator (a blank space in our case). It directs the split method to divide the string each time it spots the separator. Consequently, it returns the resulting substrings as elements of an array.

Let us breakdown these steps


// Declare a string
var str = "Hello World";
// Use split method on the string
var res = str.split(" ");

After executing this code, the variable `res` would contain an array – [“Hello”, “World”].

Split Function and Vue.js

In the context of Vue.js, you might want to incorporate this functionality within your Vue instance or component. For example, you might store the string value in your data object and create a method to split the string


new Vue({
  el: '#app',
  data: {
    message: 'Hello World'
  methods: {
    splitMessage: function() {
      this.message = this.message.split(" ");

Here we’ve created a Vue instance, with a data object containing the message string. We’ve also added a method – `splitMessage`. This method is the key piece for converting the string into an array. It alters the `message` into an array by applying the `split()` method.

Note that the use of this in `splitMessage` refers to the Vue instance, thus `this.message` references the `message` property in the data object. The concept is very underpinning and quite applicable in many other settings involving manipulating data for user interfaces.


In summary, JavaScript offers a straightforward approach in Vue.js to convert strings into arrays using the `split()` function. Understanding this provides a crucial foundation in manipulating data for user interfaces, an aspect that is particularly helpful in many different contexts. In addition to that, the versatility of Vue.js helps in combining JavaScript’s core concepts and Vue’s reactivity system, thus paving the path to creating more dynamic and interactive web applications.

Related posts:

Leave a Comment