I'm iterating an array and based on a condition I want to delete the specific element from that array based on index(currently I use). Following is my code which is working perfectly as expected.
var todayWeekNo = new Date().getWeek();
$.each(arr, function (i, j) {
var isEqual = todayWeekNo == new Date(j.Date).getWeek();
if (isEqual) {
delete arr[i];
}
});
You can see this working fiddle
While looking for a better approach, I came to know that
Delete won't remove the element from the array it will only set the element as undefined.
So I replaced delete arr[i];
with arr.splice(i, 1);
For first 2 iteration it was working fine, whereas it get stuck at the last iteration.
Below is console message(jsfiddle):
index0 arr: [object Object] (index):43
index1 arr: [object Object] (index):43
index2 arr: undefined (index):43
Uncaught TypeError: Cannot read property 'Date' of undefined
Please shed some light on this issue.