0

I have an array and I want to get the minimum of all stardates and maximum of all end dates. I am sorting the array first and then getting min and max of it. Below is the code.

var ticket_arr = '[{"Id":"a874062a-3f1e-4f91-a9d7-334aedd2e79f","Name":"Tesr","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"19f3722f-5f19-430a-bf8a-699c6841aec2","Name":"Go define this test projecct","StartDate":"2015-04-21","EndDate":"2015-04-29","Duration":"8","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5576edbf-3feb-4204-b488-0d0a35688538","Name":"Mike is getting a lesson in cool","StartDate":"2015-04-08","EndDate":"2015-04-10","Duration":"2","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5bf5d77c-73b1-480b-8758-2e822db73964","Name":"Test ticket","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false}]';

function sortArray(arr, name){
    arr.sort(function(a,b) 
        {
      if (new Date(a[name]) > new Date(b[name]) )
         return 1;
      else if (new Date(a[name]) < new Date(b[name]) )
         return -1;
       else
         return 0;
    }); 
}
function getMinMaxDate(arr, name, type)
{
    if(arr.length)
    {
        sortArray(arr, name);
        console.log(arr);
        console.log(name);
        if(type == "Min" && arr.length)
        {
                        console.log(arr[0][name]);
            return arr[0][name];
        }

        if(type == "Max" && arr.length)
        {
            return arr[arr.length-1][name];
        }
    }
    else
        return '';
}

But the code gives an error that says arr.sort is not a function.

2
  • 5
    Your ticket_arr is not an array but JSON, you need to parse it first with JSON.parse. Commented Apr 27, 2015 at 10:22
  • Your "array" seems to be a string, in this case. Commented Apr 27, 2015 at 10:23

1 Answer 1

1

That's because ticket_arr isn't an array, it's a string. Fix that either by removing the single quotes around the value:

var ticket_arr = [{"Id":"a874062a-3f1e-4f91-a9d7-334aedd2e79f","Name":"Tesr","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"19f3722f-5f19-430a-bf8a-699c6841aec2","Name":"Go define this test projecct","StartDate":"2015-04-21","EndDate":"2015-04-29","Duration":"8","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5576edbf-3feb-4204-b488-0d0a35688538","Name":"Mike is getting a lesson in cool","StartDate":"2015-04-08","EndDate":"2015-04-10","Duration":"2","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5bf5d77c-73b1-480b-8758-2e822db73964","Name":"Test ticket","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false}];

Or, if it's loaded via AJAX or generated on the client side, by parsing it as JSON:

var ticket_arr = JSON.parse('[{"Id":"a874062a-3f1e-4f91-a9d7-334aedd2e79f","Name":"Tesr","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"19f3722f-5f19-430a-bf8a-699c6841aec2","Name":"Go define this test projecct","StartDate":"2015-04-21","EndDate":"2015-04-29","Duration":"8","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5576edbf-3feb-4204-b488-0d0a35688538","Name":"Mike is getting a lesson in cool","StartDate":"2015-04-08","EndDate":"2015-04-10","Duration":"2","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false},{"Id":"5bf5d77c-73b1-480b-8758-2e822db73964","Name":"Test ticket","StartDate":"2015-04-08","EndDate":"2015-04-09","Duration":"1","leaf":true,"IsProject":false,"IsStage":false,"IsActivity":false,"IsTicket":true,"IsTask":false}]');

(If you intend to use JSON.parse on older browsers, such as IE7-, you'll need a polyfill. JSON 3 is a well-known one.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.