Skip to main content

I am trying to update the value of my object elements whenever one of the object is deleted. However, I always get the Uncaught ReferenceError: i is not defined error on my console. 

Here is my code 

deleteTask(event) {

this.todoTasks = this.todoTasks.filter(task => task.id !== event.target.name);

this.updateId();

console.log(this.todoTasks);

}

updateId(){

for(i=1; i<=this.todoTasks.length; i++){

this.todoTasks[i-1].id = i;

}

}
1 件の回答
  1. 2022年1月30日 12:02
    Hi  Roman,

    You have missed declaring the variable by using either var or let for variable i

    . Here is your updated code:

    deleteTask(event) {

    this.todoTasks = this.todoTasks.filter(task => task.id !== event.target.name);

    this.updateId();

    console.log(this.todoTasks);

    }

    updateId(){

    for(var i=1; i<=this.todoTasks.length; i++){

    this.todoTasks[i-1].id = i;

    }

    }

    Thanks

    Gulshan Raj