1) Using $.each()
iterate the parsed JSON.
2) To get the week number use the below protype method
Date.prototype.getWeek = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}
To use:
var todayWeekNo = new Date().getWeek();
3)To check whether they belong to same week use
var isEqual = (todayWeekNo == new Date(j.Date).getWeek());
4) If it is not equal, delete it using index.
Finally,
Date.prototype.getWeek = function () {
var onejan = new Date(this.getFullYear(), 0, 1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);
}
var arr = [{
"StudentID": 5041,
"Status": "Joshua picked up from school [0] at 12:41PM and reached home [0] at 12:43PM",
"Date": "2013-11-20"
}, {
"StudentID": 5042,
"Status": "Joshua picked up from school [0] at 12:41PM and reached home [0] at 12:43PM",
"Date": "2013-11-20"
}, {
"StudentID": 5043,
"Status": "Joshua picked up from school [0] at 12:41PM and reached home [0] at 12:43PM",
"Date": "2013-11-20"
}];
var todayWeekNo = new Date().getWeek();
$.each(arr, function (i, j) {
var isEqual = todayWeekNo == new Date(j.Date).getWeek();
if (!isEqual) {
delete arr[i];
}
});
Updates:
Since Delete
won't remove the element from the array it will only set the
element as undefined
.
So I tried using arr.splice(i, 1);
but it was not working. With reference to this question here is an alternative approach.
var todayWeekNo = new Date().getWeek();
for (var i = 0; i < arr.length;) {
var isEqual = (todayWeekNo == new Date(arr[i].Date).getWeek());
if (!isEqual) {
arr.splice(i, 1);
} else {
i++;
}
}
Hope you understand.