Suppose you had an array like:
var arr= [{id:121, v:'a'}, {id:232, 'b'}];
And you need to find id: 232 and delete it so you can:
for (var i = arr.length; i--;) {
if (arr[i].id === 232) {
arr.splice(i, 1);
}
};
And let there be an event handler that added elements to the array, for example:
arr.push( {id:443, 'c'} );
Is it possible that an event handler can be called as a for loop repeats? If so, then splicing (i, 1) will remove the invalid array index.
As javascript is single-threaded, is it sufficient enough to complete the for-loop before handling events?
source
share