Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!