How to store multiple strings in an array in VueJS?

Hi Friends 👋,

Welcome To SortoutCode! ❤️

To store multiple strings in an array in VueJS. There are several methods to store multiple strings in an array, we are going to use arry.push(str1,str2,...) and spread operator [...arry,str1,str2,...] methods.

Short solution

parentComp.vue

var Arry = ["Sunday", "Monday"]; // Array

ArryA = [...Arry,"Thursday", "Friday", "Saturday"]
console.log(ArryA); //  [ "Sunday", "Monday", "Thursday", "Friday", "Saturday" ]

ArryB = Arry.push("Thursday", "Friday", "Saturday")
console.log(ArryB); //  [ "Sunday", "Monday", "Thursday", "Friday", "Saturday" ]

Today, I am going to show you How you store multiple strings in an array in VueJS?

Table of Content

  • Setup the Vue (Optional)
  • Create FirstComponent.vue and import it into App.js
  • Storing multiple strings into array using spread operator
  • Storing multiple strings in array using the push() method

This article will guide you to How do I store multiple strings in an 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>

Storing multiple strings into an array using the spread operator

To store multiple strings in an array in vuejs. We are going to define the array of string Arry in the data property. And we are going to store multiple strings into an array using the spread operator `[…arry,str1,str2,…].

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <div>
      <p>Array : {{ Arry }}</p>
      <p v-if="output">Output: {{ output }}</p>
      <button @click="convertMethodA">Convert</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      Arry: ["Sunday", "Monday"],
      output: "",
    };
  },
  methods: {
    convertMethodA() {
      this.output = [...this.Arry, "Thursday", "Friday", "Saturday"];
    }
  },
};
</script>

For now, let’s check the output.

Output

store multiple string in an array

Storing multiple strings in an array using the push() method

To store multiple strings in an array in vuejs. We are going to define the array of string Arry in the data property. And we are going to store multiple strings into an array using the push method Arry.push(str1,str2,...).

Let’s take a short example to better understand.

FirstComp.vue

<template>
  <div class="app-data">
    <h1>Welcome to sortoutcode.com</h1>
    <div>
      <p>Array : {{ Arry }}</p>
      <p v-if="output">Output: {{ output }}</p>
      <button @click="convertMethodB">Convert</button>
    </div>
  </div>
</template>
<script>
export default {
  name: "FirstComp",
  data() {
    return {
      Arry: ["Sunday", "Monday"],
      output: "",
    };
  },
  methods: {
    convertMethodB() {
      var finalary = this.Arry.push("Thursday", "Friday", "Saturday");
      this.output = finalary;
    },
  },
};
</script>

For now, let’s check the output.

Output

store multiple string in an array

Links

Here, is the above program code sandbox link of how to store multiple strings in 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