+ Start a Discussion
Roman RegmiRoman Regmi 

For loop not working inside LWC javascript.

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;
}
}

Best Answer chosen by Roman Regmi
GulshanRajGulshanRaj
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