0
"[
"{\"StudentID\":5042,\"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\":5042,\"Status\":\"Joshua picked up  from school [0]    at 12:41PM and reached home [0] at    12:43PM\",\"Date\":\"2013-11-20\"}"]

I have this type of data in java script i want to delete that records which are not in current week date.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Zeeshan Saleem
  • 149
  • 4
  • 15
  • 3
    You have to parse the JSON, iterate over the resulting array and compare the value of the properties. Maybe this gives you a start: http://stackoverflow.com/q/11922383/218196. – Felix Kling Nov 21 '13 at 08:07
  • 1
    I feel something wrong in your JSON, can you correct it. – Praveen Nov 21 '13 at 08:17
  • dont know that this is correct or not but i am doing lots of other things with this data and Json.Parse() easily parse my above data so seems to be no issue in Json format – Zeeshan Saleem Nov 21 '13 at 08:57

2 Answers2

1

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

JSFiddle

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.

Community
  • 1
  • 1
Praveen
  • 55,303
  • 33
  • 133
  • 164
0

Call this code to reduce as per your need.

function reduceJSON(data) {
    $.each(data, function(key, value) {
        var returnObject = {};
        if(/* Your Condition Here */) {
            returnObject.push(value);
        }
    });
    return returnObject;
}

yourJSON = reduceJSON(yourJSON);
Bikas
  • 2,709
  • 14
  • 32