B
B
bormor2018-06-26 13:07:29
Vue.js
bormor, 2018-06-26 13:07:29

Getting to know Vue. Is this method for working with data done correctly?

Getting to know Vue.
I am writing the toggleTask method for ToDo (i.e. marks done / not done).
Is this solution correct? Are there any Vue-way ways to improve it?

methods:{
    toggleTask(id){
      let taskIndex;
      this.tasks.forEach( (item, index) => {
        if ( item.id === id){
          taskIndex = index
        }
      });

      this.tasks[taskIndex].completed = !this.tasks[taskIndex].completed;
    }
  },

As I understand it, if immutable data operations are preferable in react, then here they are mutable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Koteezy, 2018-06-26
@bormor

You can just pass the entire task, and it all boils down to:
task.completed = !task.completed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question