Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
Vue how to split string by comma and save as array

I'm a Ruby guy who is trying to get some Vue knowladge. In my projecy user can provide multiple styleCodes via input field - each of the styleCode is separated by a comma. I want to store this styleCodes in array to calculate its length freely. I've below:

<template>
  <form>
    <input type="text" v-model="tempStyleCodes">
  </form>
  <button
    type="button"
    @click="syncProducts"
  >
    Sync
  </button>
</template>

<script>

export default {
  name: 'SyncProducts',
  data() {
    return {
      styleCodes: [],
      tempStyleCodes: '',
    }
  },
  computed: {
    productsToSyncAmount () {
      this.tempStyleCodes.split(',')
      this.styleCodes.push(this.tempStyleCodes)
      return this.styleCodes.length
    }
  },
  methods: {
    async syncProducts() {
      let confirmationText = `Do you want to ${this.productsToSyncAmount} sync products?`
      this.loadId = null

      if (this.productsToSyncAmount === 0) {
        ModalController.showToast('', 'Type product codes for sync first, please!', 'warning')
      }
      // (...) some ohter irrelevant code
    },
  }
}

I think I'll need something similar to Ruby method .split(',') because my code for sample input of 4321test, test, 908test produces:

styleCodes: [ "4321test, test, 908test" ]

Where length will give me 1 element instead of 3.

So desired result should be:

styleCodes: [ "4321test", "test", "908test" ]

How to split these values?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

as you are using the split function to check the length of the string i assume your issue isn't how to split the value but how to bind to it

in which case i suggest use a writable computed value

computed: {
  formattedStyleCodes :{
    get(){
      return this.styleCodes.join(",");
    },
    set(value){
      this.styleCodes= value.split(',');
    }
  }
},

you can then bind to it as

<input type="text" v-model="formattedStyleCodes ">
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use String's split() method, which can take either a string or a regular expression as the separator.

const codes = this.tempStyleCodes.split(/\s*,\s*/);

Now that you have an array of codes, you can push all of them in styleCodes by using the spread syntax (see browser compatibility) which is accepted by Array's push() method:

this.styleCodes.push(...codes);

Example:

const styleCodes = [];
const tempStyleCodes = "4321test, test, 908test";

const codes = tempStyleCodes.split(/\s*,\s*/);
styleCodes.push(...codes);

console.log(styleCodes);

about 4 years ago · Juan Pablo Isaza Relatório

0

watch: {
  tempStyleCodes(newVal){
     if(newVal) this.styleCodes =  newVal.split(',')
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda