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

120
Visualizações
Am I screwing Vue.js' reactivity by looping over an array in the state and creating new properties inside every element?

I am executing the following code in my mounted() method:

    mounted(){
        this.sentences = this.sentences.map((sentence, index) => {
            let words = [];
            sentence.text.split(' ').forEach((word, i) => {
                words.push({
                    text: word,
                    selected: false
                })
            });
            sentence.words = words
            sentence.grade.selected = false;

            return sentence;
        })
    }

Originally, this.sentences looks like this:

sentences: [
                {
                    text: "Go away and hide behind that door where we found you just now.",
                    grade: {
                        text: 606
                    }
                },
                {
                    text: "Please don't let anyone spoil these nice fresh flowers.",
                    grade: {
                        text: 609
                    }
                },
                {
                    text: "The string had eight knots in it which I had to untie.",
                    grade: {
                        text: 700
                    }
                },
]

So I am adding a property words to each object in the sentences array, and a property selected to each object's grade property. After swapping from hard coded words and selected properties to adding them dynamically with JavaScript, the reactivity for words stopped working. I assume I'm not setting them the right way but I can't figure out what I'm doing wrong.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The issues is limitations when adding new properties to a reactive object.

Use Vue.set to add new properties.

Vue.set(sentence, 'words', words)
Vue.set(sentence.grade, 'selected', false)

Or use a computed function.

computed: {
  sentencesComputed() {
    return this.sentences.map((sentence, index) => {
            let words = [];
            sentence.text.split(' ').forEach((word, i) => {
                words.push({
                    text: word,
                    selected: false
                })
            });
            sentence.words = words
            sentence.grade.selected = false;

            return sentence;
        })
  }
}
over 4 years ago · Santiago Trujillo 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