Javascript for loop skips last element -
i have array deleting elements on base of conditions , when delete element restart loop because index refreshed.
var k; for( k=0 ; k < this.j_data.length;k++){ if(condition === true){ this.j_data.splice(k, 1); k = 0; // restart } }
my array this.j_data have 2 element both should deleted splice after first element deleted , last 1 skipped loop.
any idea missing
thanks @jaromanda x
k++ occur first k=-1 fix issue
var k; for( k=0 ; k < this.j_data.length;k++){ if(condition === true){ this.j_data.splice(k, 1); k = -1; // restart } }
Comments
Post a Comment