I have lightning controller in were I want to filter a list based on a datefield. I tried the following but end up with an empty list:

My list has 2 records:

serverList[0].Startdatum__c = 1-12-2018
serverList[1].Startdatum__c = 30-7-2018

startdate = 02-08-2018

var serverListFilteredbyDate = serverList.filter(function(item) { return item[(serverList[i].Startdatum__c >= startDate)]});   
  • 1
    Should not it be like - var serverListFilteredbyDate = serverList.filter(function(item) {return item.Startdatum__c >= startDate;}); ? – N.. Jun 27 '18 at 13:33

As written, you're trying to subtract numbers (02-08-2018 = -2024). Instead, you need to make sure you're using Date objects:

startdate = new Date("02-08-2018")

I believe you'll also need to do this for the values in the field as well.

As N.B. commented while I was writing, you use "item" (the variable) to do the filtering. You could actually write it more like this:

startdate = new Date("02-08-2018")
var serverListFilteredbyDate = serverList.filter(item => new Date(item.Startdatum__c) >= startDate);   
  • I tried your suggestion but I end up with an "item is not defined" – Thomas Jun 27 '18 at 14:04
  • I just wrote similar code and tested it just now, it works for me. – sfdcfox Jun 27 '18 at 14:10
  • When I replace item with serverList it works. Thanks! – Thomas Jun 27 '18 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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