0

My array:

 urlArray =  dataArray.map(function(item){
              return {
               Id:                               item.Id,
               Title:                            item.Title, 
               StartDate:                        new Date(item.Start_x0020_Date).toLocaleDateString("en-GB"),                       
               EndDate:                          new Date(item.End_x0020_Date).toLocaleDateString("en-GB"),                                 
                };   
          });

var nextYear   = '01/01/2019';

How can I return return all items that have a start date that begins in this year (2018)? This is the same as excluding all items which have a start date > next year (i.e. 2019).

urlArray returns each date format as so: 1/1/1970 whereas in browser tools the API returns a date format of:

"2018-12-26T08:00:00Z"

I have tried returning all items with a filter that have a start date less than next year but still not working. My attempt is below:

let filteredAZLista = urlArray.filter(c =>  c.StartDate < nextYear);

1 Answer 1

0

You should do your comparisons by using Date objects, not strings, and you could do your filter before you do your string conversions in your map function, that way you are mapping over already filtered data.

So something like this:

var nextYear = new Date('01/01/2019');

var filteredData = dataArray.filter(item => new Date(item.Start_x0020_Date) < nextYear);

urlArray =  filteredData.map(function(item){
    return {
     Id:                               item.Id,
     Title:                            item.Title, 
     StartDate:                        new Date(item.Start_x0020_Date).toLocaleDateString("en-GB"),                       
     EndDate:                          new Date(item.End_x0020_Date).toLocaleDateString("en-GB"),                                 
      };   
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.